我有一个MapActivity,我获取当前位置,并在其上固定一个drawable。问题是,当我的位置发生变化时(onchangedlocation方法),地图上会出现另一个精确点,但第一个不会消失。另一个问题是,在我触摸屏幕之前,onchangedlocation不会固定另一个drawable。我该怎么办才能立即出现。我想用这个可绘画显示汽车的当前位置,以便它应该正常移动,如GPS或其他东西。
这是我添加精确定位的活动。
public class CustomPinpoint 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);
c = 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();
}
}
我在我的oncreate方法中使用它:
lm= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
towers = lm.getBestProvider(crit, false);
final Location location = lm.getLastKnownLocation(towers);
if(location != null){
latit = (int) (location.getLatitude() *1E6);
longi = (int) (location.getLongitude() *1E6);
ourLocation = new GeoPoint(latit, longi);
controller.animateTo(ourLocation);
controller.setZoom(14);
if//myItemizedOverlay.addItem(ourLocation, "myPoint1", "myPoint1");
OverlayItem overlayItem = new OverlayItem(ourLocation, "What's up", "2nd String");
CustomPinpoint custom = new CustomPinpoint(d, Map.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);}
map.setSatellite(false);
map.setStreetView(true);
这是我的onlocationchanged
public void onLocationChanged(Location l) {
// TODO Auto-generated method stub
latit = (int) (l.getLatitude() *1E6);
longi = (int) (l.getLongitude() *1E6);
GeoPoint ourLocation = new GeoPoint(latit, longi);
OverlayItem overlayItem = new OverlayItem(ourLocation, "What's up", "2nd String");
CustomPinpoint custom = new CustomPinpoint(d, Map.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
}
我认为对于我当前位置的多个drawable,一个解决方案是只显示添加的最后一个精确点(geopoint),但我真的不知道该怎么做。
如果你能解决这两个问题,我将非常感激。谢谢!
PS:当设备放在汽车里或者速度很快的东西时,我需要看起来像是可以继续移动。
答案 0 :(得分:3)
In the onlocationchaged method,add the following line,so that it will make your previous pin disappear..if(!mapOverlays.isEmpty())
{
mapOverlays.clear();
mapView.invalidate();}
答案 1 :(得分:1)
我在代码中没有看到overlayList的定义,但我想它必须是ArrayList<OverlayItem>
类型
因此,在overlayList.add(自定义)之前,您需要调用overlayList.clear()。这将为您留下最新的位置标记。至于更新显示器,我会认为它应该在你.add时这样做。我想你总是可以在mapview上调用.invalidate()来查看是否有帮助