2011-11-09 72 views
0

看過很多不同的指南/教程後,我發現自己迷失於如何在用戶單擊地圖上的疊加層(標記)時實施簡單警報框/工具提示。在疊加式水龍頭上顯示工具提示/警示

我看過的所有指南都讓人感到困惑,因爲在我已經擁有的代碼中實現它。這些標記來自外部JSON,我可以獲取緯度/經度來創建標記並將其放置在地圖上。到目前爲止這麼好,我擁有所有的標記,但當用戶點擊它們時,我似乎無法找到實現警告框/工具提示的最佳方式。

我的代碼張貼在下面...我知道這是很多的東西,但任何幫助表示讚賞。非常非常感謝你 !

package ca.transcontinental.android.vanilla.demo;

import (...) 

public class GoogleMapActivity extends MapActivity implements LocationListener { 

    MapView mapView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.location_map_layout); 
     LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 

     this.mapView = (MapView) findViewById(R.id.mapview); 
     this.mapView.setBuiltInZoomControls(true); 
     this.mapView.getController().setZoom(10);    
    } 

    private BaseApplication getBaseApplication() { 
     return (BaseApplication)getApplication(); 
    } 

    public void onLocationChanged(Location location) { 
     if (location != null) { 

      String sLat = String.valueOf(location.getLatitude()); 
      String sLng = String.valueOf(location.getLongitude()); 

      try { 
       getBaseApplication().debug("GPS: lat="+sLat + ", lng="+sLng);      
       RestQueryEngine.getInstance().setup("http://example.com/JSONPublicationService.svc", "key", getBaseApplication().getOrgCode(), getBaseApplication().getBannerCode()); 
       StoreList list = StoreList.getStoreListByGeopos(sLat, sLng);     
       for(Store store: list){     
        System.out.println(store); 
        MapOverlay overlay = new MapOverlay(Double.parseDouble(store.getLatitude()), Double.parseDouble(store.getLongitude()), ""); 
        mapView.getOverlays().add(overlay);     
       } 
       mapView.invalidate(); 
      } 
      catch (Throwable e) { 
       e.printStackTrace(); 
       getBaseApplication().showExceptionMessage(e); 
      } 


      LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      lm.removeUpdates(this); 

     } 

    } 


    public void onProviderDisabled(String provider) { 
    } 

    public void onProviderEnabled(String provider) { 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 

    class MapOverlay extends Overlay { 
     private GeoPoint locpoint; 
     private String label; 

     public MapOverlay(GeoPoint geoPoint, String name) { 
      this.locpoint = geoPoint; 
      this.label = name; 
     } 
     public MapOverlay(double lat, double lon, String name) { 
      this(new GeoPoint((int)(lat*1E6), (int)(lon*1E6)), name);   
     }   

     @Override 
     public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { 
      super.draw(canvas, mapView, shadow); 
      Point screenPoint = new Point();    
      mapView.getProjection().toPixels(this.locpoint, screenPoint); 
      Bitmap markerImage = BitmapFactory.decodeResource(getResources(), R.drawable.androidmarker); 
      canvas.drawBitmap(markerImage, screenPoint.x - markerImage.getWidth()/2, screenPoint.y - markerImage.getHeight()/2, null); 
      return true; 
     }  

    } 
} 

回答

0

我做的是使用OverlayItem而不是疊加,並創建一個擴展ItemizedOverlay

class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> { 

    private ArrayList<MapOverlay> mOverlays = new ArrayList<MapOverlay>(); 

    public CustomItemizedOverlay(Drawable defaultMarker) { 
     super(boundCenterBottom(defaultMarker)); 
    } 

    public void addItem(MapOverlay item) { 
     mOverlays.add(item); 
    } 

    @Override 
    protected MapOverlay createItem(int i) { 
     return mOverlays.get(i); 
    } 

    @Override 
    public int size() { 
     return mOverlays.size(); 
    } 

    @Override 
    protected boolean onTap(int index) { 
     // Handle tap for the given overlay item based on this index 
     return super.onTap(index); 
    } 
} 

這有您將使用R.drawable.androidmarker作爲默認的標記,並沒有上攻類的方式在Overlay中實現draw方法是爲您完成的。