Domanda

Come faccio ad aggiungere un pennarello su una particolare posizione nella mappa?

ho visto questo codice che mostra le coordinate della posizione toccata. E voglio un marcatore al pop o essere mostrato in quella stessa posizione ogni volta che viene toccato. Come faccio a fare questo?

 public boolean onTouchEvent(MotionEvent event, MapView mapView) {   
                if (event.getAction() == 1) {                
                    GeoPoint p = mapView.getProjection().fromPixels(
                        (int) event.getX(),
                        (int) event.getY());
                        Toast.makeText(getBaseContext(), 
                            p.getLatitudeE6() / 1E6 + "," + 
                            p.getLongitudeE6() /1E6 , 
                            Toast.LENGTH_SHORT).show();

                        mapView.invalidate();
                }                            
                return false;
            }
È stato utile?

Soluzione

Si desidera aggiungere un OverlayItem . Il tutorial Google MapView mostra come utilizzare esso.

Altri suggerimenti

Se si vuole aggiungere un marcatore nella posizione toccata, allora si dovrebbe effettuare le seguenti operazioni:

public boolean onTouchEvent(MotionEvent event, MapView mapView) {              
        if (event.getAction() == 1) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());
                    Toast.makeText(getBaseContext(),                             
                        p.getLatitudeE6() / 1E6 + "," + 
                        p.getLongitudeE6() /1E6 ,                             
                        Toast.LENGTH_SHORT).show();
                    mapView.getOverlays().add(new MarkerOverlay(p));
                    mapView.invalidate();
            }                            
            return false;
        }

Controllare che Im chiamare MarkerOverlay dopo gli appare il messaggio. Al fine di rendere questo funziona, è necessario creare un altro Overlay, MapOverlay:

class MarkerOverlay extends Overlay{
     private GeoPoint p; 
     public MarkerOverlay(GeoPoint p){
         this.p = p;
     }

     @Override
     public boolean draw(Canvas canvas, MapView mapView, 
            boolean shadow, long when){
        super.draw(canvas, mapView, shadow);                   

        //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

        //---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), /*marker image*/);            
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
        return true;
     }
 }

Spero che hai trovato utile!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top