2010-01-31 5 views
9

지도의 특정 위치에 마커를 추가하려면 어떻게해야하나요?android의 Google지도를 사용하여 터치 한 위치에 마커 추가

이 코드는 터치 된 위치의 좌표를 보여줍니다. 그리고 마커를 터치하거나 터치 할 때마다 같은 위치에 표식을 표시하려고합니다. 어떻게해야합니까?

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; 
      } 

답변

4

OverlayItem을 추가하려고합니다. Google Mapview tutorial에 사용 방법이 나와 있습니다. 당신이 접촉 위치에 마커를 추가하려면

+0

oks! 알았다. 이제 마커를 그릴 수 있습니다. 고마워 :) – lulala

+0

위대한, 당신이 대답을 받아 들일 수 있도록 질문에 대답하고자하는 다른 사람들이 이미이 답변을 알고 있다는 것을 알 수 있습니까? – RickNotFred

8

후 다음을 수행해야합니다 메시지가 나타난 후 임 MarkerOverlay를 호출하는 것을

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; 
     } 

확인. 위해 는 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; 
    } 
} 

난 당신이 유용 희망!

관련 문제