2010-02-11 9 views
10

Dites que j'ai le contrôle mapview dans mon application Android. Si je sais qu'il existe un point de repère à une certaine latitude et longitude, comment puis-je déterminer si ce point de repère est actuellement visible sur l'écran de l'utilisateur? Existe-t-il une méthode pour obtenir les coordonnées des coins supérieur gauche et inférieur droit de la zone visible?Comment puis-je déterminer si un géopoint est affiché dans la zone actuellement visible?

+0

Conseil, faites attention à ce sujet: http://stackoverflow.com/questions/ 2667386/mapview-getlatitudespan-and-getlongitudespan-not-working –

Répondre

6

Quelque chose comme ça fera l'affaire.

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, car il m'a fallu un certain temps pour trouver une solution de travail simple, je vais poster ici pour tous après moi;) Dans votre propre sous-classe de MapView il suffit d'utiliser les éléments suivants:

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

C'est tout, alors vous pouvez obtenir les coordonnées via

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

et

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

Veuillez arrêter de le rediffuser exactement répondre à plusieurs messages. S'ils sont des doublons, marquez-les comme tels. Si ce n'est pas le cas, adaptez votre réponse à la question spécifique posée. –

+0

Cela n'a pas fonctionné. –

1

Rejoindre les conseils here, here et here:

Voici le code complet de mon 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)); 
    } 
} 
Questions connexes