标记未显示在当前位置

时间:2012-08-12 08:32:51

标签: java android android-maps

我需要使用current locationMarker上始终显示我的Google Map作为ItemizedOverlay,但每当我运行以下代码时,我的标记都没有&#39} ;如果我从DDMS更改位置,则显示。以下是我的代码。我在做什么事吗?

public class MainActivity extends MapActivity {
    private MyItemizedOverlay myItemizedOverlay = null;
    private LocationManager locationManager = null;
    private MapView mapView = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, new GeoUpdateHandler());

    }


    public class GeoUpdateHandler implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude() * 1E6);
        int lng = (int) (location.getLongitude() * 1E6);

        Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
        int markerWidth = marker.getIntrinsicWidth();
        int markerHeight = marker.getIntrinsicHeight();
        marker.setBounds(0, markerHeight, markerWidth, 0);


        myItemizedOverlay = new MyItemizedOverlay(marker);
        mapView.getOverlays().add(myItemizedOverlay);


        GeoPoint point = new GeoPoint(lat, lng);

        //GeoPoint myPoint1 = new GeoPoint((int) (37.347184*1000000), (int) (-121.966551*1000000));
        myItemizedOverlay.addItem(point, "myPoint1", "myPoint1");
        mapController.animateTo(point);
        String address = ConvertPointToLocation(point);
        Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

        mapView.invalidate();

    }


}

以下是MyItemizedOverlay class

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{

    private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();

    public MyItemizedOverlay(Drawable marker) {
        super(boundCenterBottom(marker));
        // TODO Auto-generated constructor stub

        populate();
    }

    public void addItem(GeoPoint p, String title, String snippet){
        OverlayItem newItem = new OverlayItem(p, title, snippet);
        overlayItemList.add(newItem);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return overlayItemList.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return overlayItemList.size();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        // TODO Auto-generated method stub
        super.draw(canvas, mapView, shadow);
        //boundCenterBottom(marker);
    }

}

因为我需要在Google地图上显示其他标记,所以这就是我使用ItemizedOverlay概念的原因。首先,每当我的应用程序启动时,我都需要始终使用ItemizedOverlay关注我当前的位置。对我的例子的任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果您只想显示用户的当前位置,请使用MyLocationOverlay。它专为此而设计。 Here是如何使用它的一个很好的例子,下面是基本的中央代码片段:

// This goes into the onCreate method
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);

myLocationOverlay.runOnFirstFix(new Runnable() {
  public void run() {
    mapView.getController().animateTo(myLocationOverlay.getMyLocation());
     }
}); 

请参阅教程以获取更详细的示例。

答案 1 :(得分:0)

GeoPoint point = new GeoPoint(lat, lng);
change above code

declare global variable
GeoPoint point;

then assign onLocationChanged() method 

point = new GeoPoint(lat, lng);

按照非常简单的示例链接

http://android-coding.blogspot.com/2011/06/using-itemizedoverlay-to-add-marker-on.html

相关问题