0

我有一张地图,目前我经常从用户的 GPS 位置在地图上放置一个自定义图钉,这也取决于他们的位置何时发生变化,但 GPS 在这里有点不稳定很多。目前,每次发生这种情况时,都会在地图上放置一个新图钉。我想删除前一个图钉,以便在地图上只显示用户位置的一个图钉,就像用户位置的实时表示一样。

除此之外。是否可以在地图上有一组单独的图钉(它们的位置永远不会改变)并确保在用户位置更新时它们也不会被清除。

对代码的任何其他评论将不胜感激。

Imports...

public class MapOne extends MapActivity implements LocationListener {

... fields


@Override
protected void onCreate(Bundle icicle) {
    ....onCreate initialisations
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    compass.disableCompass();
    lm.removeUpdates(this);
    super.onPause();

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    compass.enableCompass();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 20, this);
    super.onResume();
}

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

public void onLocationChanged(Location l) {
    // TODO Auto-generated method stub
            lat = (int)(l.getLatitude() *1E6);
            longi = (int)(l.getLongitude() *1E6) ;

            GeoPoint ourLocation = new GeoPoint(lat, longi);
            OverlayItem overlayItem = new OverlayItem(ourLocation, "Hey!", "What's up!?");
            CustomPinpoint custom = new CustomPinpoint(customMarker, Map.this);
            custom.insertPinpoint(overlayItem);
            overlayList.add(custom);

}

}

如果需要,请查明类以供参考。

public class Pinpoint extends ItemizedOverlay<OverlayItem> {

private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context c;

public CustomPinpoint(Drawable defaultMarker) {
    super(boundCenter(defaultMarker));
    // TODO Auto-generated constructor stub
}

public CustomPinpoint(Drawable m, Context context) {
    // TODO Auto-generated constructor stub
    this(m);
    setC(context);
}

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

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

public void insertPinpoint(OverlayItem item) {
    pinpoints.add(item);
    this.populate();
}

public Context getC() {
    return c;
}

public void setC(Context c) {
    this.c = c;
}

}
4

1 回答 1

0

如果你想添加一个跟随用户当前位置的 Pin,你可以使用 com.google.android.maps.MyLocationOverlay ....这也为你提供了所需的监听器。你也可以覆盖它的 onDraw() 方法来绘制您的自定义图钉。然后您可以像添加任何其他叠加层一样添加此叠加层...不要忘记调用 myLocationOverlay.enableMyLocation() 以便您收到位置更改。

于 2012-01-26T18:37:22.127 回答