2013-01-25 47 views

回答

3

您可能會覆蓋GoogleMap標記點擊事件並在那裏調整相機。

例如

Maker lastOpened = null; 

mMap.setOnMarkerClickListener(new OnMarkerClickListener() { 
    public boolean onMarkerClick(Marker marker) { 
     // Check if there is an open info window 
     if (lastOpened != null) { 
      // Close the info window 
      lastOpened.hideInfoWindow(); 

      // Is the marker the same marker that was already open 
      if (lastOpened.equals(marker)) { 
       // Nullify the lastOpened object 
       lastOpened = null; 
       // Return so that the info window isn't opened again 
       return true; 
      } 
     } 

     // Open the info window for the marker 
     marker.showInfoWindow(); 
     // Re-assign the last opened such that we can close it later 
     lastOpened = marker; 

     // Get the markers current position 
     LatLng curMarkerPos = marker.getPosition(); 

     // Use the markers position to get a new latlng to move the camera to such that it adjusts appropriately to your infowindows height (might be more or less then 0.3 and might need to subtract vs add this is just an example) 
     LatLng camMove = new LatLng(curMarkerPos.latitude + 0.3, curMarkerPos.longitude); 

     // Create a camera update with the new latlng to move to    
     CameraUpdate camUpdate = CameraUpdateFactory.newLatLng(camMove); 
     // Move the map to this position 
     mMap.moveCamera(camUpdate); 

     // Event was handled by our code do not launch default behaviour. 
     return true; 
    } 
}); 

mMap.setOnMapClickListener(new OnMapClickListener() { 

    @Override 
    public void onMapClick(LatLng point) { 
     if (lastOpened != null) { 
      // Hide the last opened 
      lastOpened.hideInfoWindow(); 

      // Nullify lastOpened 
      lastOpened == null; 
     } 

     // Move the camera to the new position 
     final CameraPosition cameraPosition = new CameraPosition.Builder().target(point).zoom(mMap.getCameraPosition().zoom).build(); 

     mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
    }); 

此代碼處理不當經過測試,但至少應該給你一個偉大的開始。 onMarkerClick的默認行爲是移動攝像頭並打開信息窗口。所以重寫這個並實現你自己的應該允許你移動你的相機。

感謝,合作舉辦一年一度的

+0

有一個在邏輯DMAN小遺漏。當用戶點擊標記以外時,lastOpened需要重置,並且信息窗口消失。否則一個好的解決方案 – oviroa

+0

@oviroa良好的捕獲,我不記得是否在MapClick上關閉了標記。我已經更新了答案(可否請您標記爲幫助他人的正確答案)。謝謝,DMan – DMCApps

+0

其實還有一件事。 lastOpened字段不是必需的。你可以檢查marker.isInfoWindowShown()。此外,lastOpened.hideInfoWindow()不是必需的,默認情況下會處理它。 – oviroa

3

我修改了非常好的答案上面的人誰只是想刪除標記點擊移動到中心的默認行爲:

一下添加到setUpMap():

mMap.setOnMarkerClickListener(getMarkerClickListener()); 

然後添加方法:

Marker lastOpened = null; 

public OnMarkerClickListener getMarkerClickListener() { 
    return new OnMarkerClickListener() { 
     public boolean onMarkerClick(Marker marker) { 
      if (lastOpened != null) { 
       lastOpened.hideInfoWindow(); 
       if (lastOpened.equals(marker)) { 
        lastOpened = null; 
        return true; 
       } 
      } 

      marker.showInfoWindow(); 
      lastOpened = marker; 
      return true; 
     } 
    }; 
} 
+1

獲取我的+1。像箱子裏的魅力那樣工作。謝謝! – PeteH

相關問題