1

我試圖在地圖上繪製一些圖標,只要觸摸它即可。我想我快到了。 這是代碼。地圖上的觸摸圖標

package com.example.googledemo4; 

import java.util.List; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.Toast; 
import android.support.v4.app.NavUtils; 

public class GoogleDemo4 extends MapActivity{ 

MapController mc; 
MapView mapView; 
GeoPoint g; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_google_demo4); 

    mapView=(MapView) findViewById(R.id.MapView); 
    mapView.setBuiltInZoomControls(true); 


    mc=mapView.getController(); 

    List<Overlay> overlays = mapView.getOverlays(); 

    GeoPoint geoPoint = new GeoPoint(18533000,73850000); 


    MapOverlay4 mapOverlay = new MapOverlay4(geoPoint,mapView,this); 

    overlays.add(mapOverlay); 

    mc.animateTo(geoPoint); 
    mc.setZoom(17); 
    geoPoint=g; 
    mapView.setOnTouchListener(touch); 

    mapView.postInvalidate(); 
    } 

private OnTouchListener touch = new OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 

     mapView=(MapView) findViewById(R.id.MapView); 

     if (event.getAction() == 1) {     
      GeoPoint geoPoint = mapView.getProjection().fromPixels(
       (int) event.getX(), 
       (int) event.getY()); 
      g=geoPoint; 
       Toast.makeText(getBaseContext(), 
        geoPoint.getLatitudeE6()/1E6 + "," + 
        geoPoint.getLongitudeE6() /1E6 , 
        Toast.LENGTH_SHORT).show(); 

     } 

     return true; 
    } 
}; 

@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 


} 

我創建了一個自定義背景畫面:

package com.example.googledemo4; 


import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.webkit.WebView.FindListener; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 

public class MapOverlay4 extends Overlay{ 

GeoPoint p ; 
MapView mapView; 
Context context; 

public MapOverlay4(GeoPoint geoPoint, MapView mapView, Context context){ 

    this.mapView=mapView; 
    this.p=geoPoint; 
    this.context=context; 
} 
public void draw(Canvas canvas, MapView mapView, boolean flag) { 
    // TODO Auto-generated method stub 
    super.draw(canvas, mapView, flag); 
     //---translate the GeoPoint to screen pixels--- 
    mapView=this.mapView; 
    Point screenPts = new Point(); 
    mapView.getProjection().toPixels(p, screenPts); 

    //---add the marker--- 
    Bitmap bmp = BitmapFactory.decodeResource(
     this.context.getResources(), R.drawable.ic_launcher);    
    canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   


} 


} 

此代碼繪製圖標在第一時間,因爲我初始化GeoPoint的一些lat和長。但在地圖被觸摸之後,什麼都沒有發生。我認爲每當地圖觸及地理位置就會改變,並且改變的地點將被提供給覆蓋層。

問候

回答

0

在onTouchListener中沒有與overlays.add(mapOverlay)類似的新Geopoint調用。做到這一點必須幫助你,我想。

+0

它工作。謝謝。 – tsogtoo

+0

偉大你整理它! – Gridtestmail

0

嘗試使用itemized overlay繪製圖像 ,並在某一點上繪製覆蓋OntouchEvent方法,並通過它的緯度和長,您將獲得的圖像上的觸摸位置 如果你需要一些例U可以看到here