Google Maps Android上的多个引脚无法正常工作

时间:2012-11-20 14:52:08

标签: android google-maps

当用户的当前位置发生变化时,我一直在使用previous questionGoogle Tutorial尝试在Google地图视图中添加多个图片图片。

点击时每个引脚都会显示引脚的经度和纬度。

但是目前我的代码只显示一个引脚,并且在用户位置发生变化时不会添加另一个图像。我对Android比较陌生,看不出为什么这段代码不起作用?

Map.java:

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

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 com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

import android.util.Log;


public class Map extends MapActivity {
   private MapView map;
   private MapController controller;
   private double lon, lat;
   private Drawable drawable;
   private MapOverlay itemizedoverlay;
   private List<Overlay> mapOverlays;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.maps);
      initMapView();
      mapOverlays = map.getOverlays();
      drawable = this.getResources().getDrawable(R.drawable.pin);
      itemizedoverlay = new MapOverlay(drawable, this);
      initMyLocation();

   }





   private void initMapView() {
      map = (MapView) findViewById(R.id.map);
      controller = map.getController();
      map.setSatellite(true);
      map.setBuiltInZoomControls(true);
   }


   private void initMyLocation() {
      final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
      overlay.enableMyLocation();
      overlay.enableCompass(); // wont work in emulator
      overlay.runOnFirstFix(new Runnable() {
         public void run() {
            // Zoom in to current location - 1 is word view
            controller.setZoom(20);
            controller.animateTo(overlay.getMyLocation());
            lon = overlay.getMyLocation().getLongitudeE6();

            lat = overlay.getMyLocation().getLatitudeE6();
            setLON(lon);
            setLAT(lat);
            //-0.959896
            //51.742953
            Log.w("test-lon", Double.toString(lon/1E6));
            Log.w("test-lat", Double.toString(lat/1E6));
            GeoPoint point = new GeoPoint((int)(lat),(int)(lon));

            mapOverlays.add(itemizedoverlay);
            test(point);
         }
      });





      map.getOverlays().add(overlay);

   }

   public void setLON(double x){ //remove?

       lon = x;

   }
 public void setLAT(double x){ //remove?

       lat = x;
   }
   public void test(GeoPoint px){

       OverlayItem overlayitem = new OverlayItem(px,"Test Point" , "Lat: " + Double.toString(lat/1E6) + "\nLon: " + Double.toString(lon/1E6) );
       itemizedoverlay.addOverlay(overlayitem);


   }


   @Override
   protected boolean isRouteDisplayed() {
      // Required method from MapActivity


      return false;
   }
}

MapOverlay.java:

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class MapOverlay extends ItemizedOverlay {

    private ArrayList<OverlayItem> mapOverlay = new ArrayList<OverlayItem>();
    private Context mContext;

    public MapOverlay (Drawable defaultMarker, Context context) {
          super(boundCenterBottom(defaultMarker));
          mContext = context;
        }

    public MapOverlay(Drawable arg0) {
        super(boundCenterBottom(arg0));

    }


    public void addOverlay(OverlayItem overlay) {
        mapOverlay.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
      return mapOverlay.get(i);
    }

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


    @Override
    protected boolean onTap(int index) {
      OverlayItem item = mapOverlay.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      return true;
    }

}

1 个答案:

答案 0 :(得分:0)

你需要实现类LocationListener来监听我们的位置变化并在地图上更新。

class LocationsListeners implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        setLAT(location.getLatitude());
        setLON(location.getLongitude());
        setGeoPoint(lat, lon);
        mapOverlays.add(geoPoint);          
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

你需要用于计算GeoPoint的方法:

    public GeoPoint setGeoPoint(double lat, double lon){
       geoPoint = new GeoPoint((int) (lat * 1E6), (int)(lon  * 1E6));
       return geoPoint;
    }

或使用Little Fluffy Location - 它的库更新我们的位置并通知你,是广播接收器,非常简单快速的实现:

尝试:

http://code.google.com/p/little-fluffy-location-library/