2

当用户的当前位置发生变化时,我一直在使用先前的问题Google 教程尝试在 Google 地图视图上添加多个图钉图像。

点击每个大头针时都会显示大头针的经度和纬度。

但是,目前我的代码仅显示一个图钉,并且在用户位置更改时不会添加另一张图像。我对 Android 比较陌生,不明白为什么这段代码不起作用?

地图.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;
    }

}
4

1 回答 1

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/

于 2012-11-20T15:48:30.153 回答