我使用List存储标记以添加到gmap并删除它自己的组件。
List<Location> listLocation = new ArrayList<Location>();
更新位置时。我是store Location to listLocation,删除旧标记。然后将最新位置添加到Gmap。
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
countUpdatePos++;
listLocation.add(location);
LatLng lastLocation = new LatLng(location.getLatitude(),
location.getLongitude());
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastLocation, 16));
String cityName = null;
Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
if (addresses.size() > 0) {
int a = addresses.get(0).getMaxAddressLineIndex();
for (int i = 0; i < a; i++) {
if (i == 0) {
cityName = addresses.get(0).getAddressLine(i);
} else {
cityName = cityName + ", "
+ addresses.get(0).getAddressLine(i);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (countUpdatePos == 1) {
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.updated_position), 2000)
.show();
progressToLoadMap.setVisibility(View.GONE);
checkStartActivity = true;
timer = new MyCount((int) totalTime * 60 * 1000, 1000);
timer.start();
}
if (listLocation.size() > 1) {
listLocation.add(location);
// gmap.clear();
LatLng newLocation = new LatLng(location.getLatitude(),
location.getLongitude());
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(newLocation, 16));
myMarker = gmap.addMarker(new MarkerOptions().position(new LatLng(
listLocation.get(listLocation.size() - 1).getLatitude(),
listLocation.get(listLocation.size() - 1).getLongitude())));
if (myMarker != null) {
myMarker.remove();
myMarker = null;
}
gmap.addMarker(new MarkerOptions()
.title(getResources().getString(
R.string.current_location_found)).snippet(cityName)
.position(newLocation));
if (listLocation.get(listLocation.size() - 1) != null
&& listLocation.get(listLocation.size() - 1) != null) {
gmap.addPolyline(new PolylineOptions()
.add(new LatLng(listLocation.get(
listLocation.size() - 1).getLatitude(),
listLocation.get(listLocation.size() - 1)
.getLongitude()),
new LatLng(location.getLatitude(), location
.getLongitude())).width(3)
.color(Color.BLUE));
}
但是当运行活动时,所有标记仍然显示,并且没有折线添加到gmap :(。帮助我 这是图片
答案 0 :(得分:4)
我讨厌同样的问题,在检查我的代码后,我发现我在oncreate和onresume事件中调用了两次添加标记的方法,因此在地图上的相同位置添加了2个标记,所以当我删除标记时使用marker.remove()方法删除一个,但另一个保留,所以我认为它不会被删除。我从oncreate事件中删除了方法调用,现在它正常工作。
答案 1 :(得分:1)
你所做的是正确的,但只需要对Marker进行一点修改: 全局设置:
Marker myMarker;
在你onCreate()
myMarker = gmap.addMarker(new MarkerOptions()
.position(new LatLng(listLocation.get(
listLocation.size() - 1).getLatitude(),
listLocation.get(listLocation.size() - 1)
.getLongitude())));
然后当您想要添加另一个标记时,请检查它是否已存在任何其他标记。如果是,则根据需要将其删除。
为此:
if(myMarker!=null)
{
marker.remove();
marker = null;
}
myMarker = gmap.addMarker(new MarkerOptions()
.title(getResources().getString(
R.string.current_location_found)).snippet(cityName)
.position(newLocation));
对于Polyline,您可以检查lat,long是否为空。
答案 2 :(得分:0)
我以前见过这样的事情。
尝试制作
//put this at the top of your class
Marker myMarker;
//this is using the reference
myMarker = gmap.addMarker(new MarkerOptions()
.title(getResources().getString(
R.string.current_location_found)).snippet(cityName)
.position(newLocation));
myMarker.remove();
实例变量。当你调用remove()时,它需要与你添加的标记相同。
确保您保留对标记的引用:
myMarker = gmap.addMarker(new MarkerOptions()
.title(getResources().getString(
R.string.current_location_found)).snippet(cityName)
.position(newLocation));
if (listLocation.get(listLocation.size() - 1) != null
&& listLocation.get(listLocation.size() - 1) != null) {
gmap.addPolyline(new PolylineOptions()
.add(new LatLng(listLocation.get(
listLocation.size() - 1).getLatitude(),
listLocation.get(listLocation.size() - 1)
.getLongitude()),
new LatLng(location.getLatitude(), location
.getLongitude())).width(3)
.color(Color.BLUE));