2013-01-23 16 views
6

Google Maps V2'yi (Android) 180 °/-180 ° boylamında (iOS MapKit gibi) sınırlayabilir miyim? Bir küme algoritması uygulamaya çalıştığım için etrafta dolaşmak istemiyorum ve 180/-180 derece bölünmesi zorlaştıracak.Android Maps - Sınırı 180 derecede

Ben kaydırmayı istiyorum kırmızı çizgisinde sınırlı olması:

enter image description here

cevap

3

Bu yüzden iyi olması gereken bir çözüm oluşturdum. Kullanıcı haritayı -180/180 sınırından kaydırırsa, harita diğer tarafa döner. Bu yüzden haritayı sarma hala mümkündür, ancak "tehlikeli" alan hiçbir zaman gösterilmez.

public class CustomMapView extends MapView { 

    private double prevLongitude; 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     boolean retVal = correctCamera(); 
     prevLongitude = getMap().getCameraPosition().target.longitude; 
     return retVal; 
    } 

    public boolean correctCamera() {  
     if (getMap().getProjection().getVisibleRegion().latLngBounds.northeast.longitude < getMap().getProjection().getVisibleRegion().latLngBounds.southwest.longitude) { 
      double diff = getMap().getProjection().getVisibleRegion().latLngBounds.southwest.longitude - getMap().getProjection().getVisibleRegion().latLngBounds.northeast.longitude; 
      double longitudeSW; 
      double longitudeNE; 

      double longitudeDiff = (360-diff)/25; 

      // use > 0 if you want the map to jump to the other side 
      // <= 0 will cause the map to flip back 
      if (prevLongitude > 0) { 
       longitudeSW = -180 + longitudeDiff; 
       longitudeNE = -180 + longitudeDiff - diff; 
      } else { 
       longitudeSW = 180 - longitudeDiff + diff; 
       longitudeNE = 180 - longitudeDiff; 
      } 
      LatLngBounds bounds = new LatLngBounds(
             new LatLng(getMap().getCameraPosition().target.latitude, longitudeSW), 
             new LatLng(getMap().getCameraPosition().target.latitude, longitudeNE) 
           ); 
      getMap().animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0)); 

      return true; 
     } 

     return false; 
    } 
} 

Ve xml:

<com.ieffects.clustermap.CustomMapView 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

Ve GoogleMap için

(mapView.getMap()):

map.setOnCameraChangeListener(new OnCameraChangeListener() { 
    @Override 
    public void onCameraChange(CameraPosition position) { 
     mapView.correctCamera(); 
    } 
}); 

Bu

Özel bir MapView oluşturmak zorunda Kullanıcı, haritanın tehlikeli bölgeye "uçmasını" sağlarsa gereklidir.

1

v2 için bu öğretici bakınız: http://econym.org.uk/gmap/range.htm - temelde, taşıma olayı için bir dinleyici ekleyin ve hareket halinde iptal aralığın dışına gider. Bu, v3'da da geçerli olmalıdır.

+0

Ama bu js'de mi? –

İlgili konular