2010-02-11 12 views
10

Supponiamo di avere il controllo mapview nella mia app per Android. Se so che esiste un punto di riferimento a una certa latitudine e longitudine, come posso determinare se quel punto di riferimento è attualmente visualizzabile sullo schermo dell'utente? Esiste un metodo per ottenere le coordinate per gli angoli in alto a sinistra e in basso a destra dell'area visibile?Come posso determinare se un geopoint è visualizzato nell'area attualmente visibile?

+0

Tip, basta prestare attenzione su questo: http://stackoverflow.com/questions/ 2667386/mapview-getlatitudespan-and-getlongitudespan-not-working –

risposta

6

Qualcosa di simile farà il trucco.

private boolean isCurrentLocationVisible() 
    { 
     Rect currentMapBoundsRect = new Rect(); 
     Point currentDevicePosition = new Point(); 
     GeoPoint deviceLocation = new GeoPoint((int) (bestCurrentLocation.getLatitude() * 1000000.0), (int) (bestCurrentLocation.getLongitude() * 1000000.0)); 

     mapView.getProjection().toPixels(deviceLocation, currentDevicePosition); 
     mapView.getDrawingRect(currentMapBoundsRect); 

     return currentMapBoundsRect.contains(currentDevicePosition.x, currentDevicePosition.y); 

    } 
-1

Ok in quanto mi c'è voluto qualche tempo per trovare una soluzione di lavoro semplice mi post qui per tutti dopo di me;) Nel vostro sottoclasse di MapView basta usare il seguente:

GeoPoint topLeft = this.getProjection().fromPixels(0, 0); 
GeoPoint bottomRight = this.getProjection().fromPixels(getWidth(),getHeight()); 

Questo è tutto, quindi è possibile ottenere le coordinate tramite

topLeft.getLatitudeE6()/1E6 
topLeft.getLongitudeE6()/1E6 

e

bottomRight.getLatitudeE6()/1E6 
bottomRight.getLongitudeE6()/1E6 
+2

Si prega di interrompere il reposting esattamente lo stesso rispondere a più post. Se sono duplicati, contrassegnali come tali. Se non lo sono, quindi adattare la risposta alla domanda specifica posta. –

+0

Non ha funzionato. –

1

Unire le punte here, here e here:

Ecco il codice completo del mio CustomMapView:

package net.alouw.alouwCheckin.ui.map; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Pair; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 

/** 
* @author Felipe Micaroni Lalli ([email protected]) 
*/ 
public class CustomMapView extends MapView { 
    public CustomMapView(Context context, String s) { 
     super(context, s); 
    } 

    public CustomMapView(Context context, AttributeSet attributeSet) { 
     super(context, attributeSet); 
    } 

    public CustomMapView(Context context, AttributeSet attributeSet, int i) { 
     super(context, attributeSet, i); 
    } 

    /** 
    * 
    * @return a Pair of two pairs with: top right corner (lat, lon), bottom left corner (lat, lon) 
    */ 
    public Pair<Pair<Double, Double>, Pair<Double, Double>> getMapCorners() { 
     GeoPoint center = getMapCenter(); 
     int latitudeSpan = getLatitudeSpan(); 
     int longtitudeSpan = getLongitudeSpan(); 

     double topRightLat = (center.getLatitudeE6() + (latitudeSpan/2.0d))/1.0E6; 
     double topRightLon = (center.getLongitudeE6() + (longtitudeSpan/2.0d))/1.0E6; 

     double bottomLeftLat = (center.getLatitudeE6() - (latitudeSpan/2.0d))/1.0E6; 
     double bottomLeftLon = (center.getLongitudeE6() - (longtitudeSpan/2.0d))/1.0E6; 

     return new Pair<Pair<Double, Double>, Pair<Double, Double>>(
       new Pair<Double, Double>(topRightLat, topRightLon), 
       new Pair<Double, Double>(bottomLeftLat, bottomLeftLon)); 
    } 
} 
Problemi correlati