我有一张从服务器加载当前地址的地图。我在MapReady上使用,在初始化之前,它从服务器加载第一个位置并缩放到该位置。我希望应用程序每隔10秒从服务器读取一次地址,并映射该位置。我在MapReady之后被困在如何删除前一个标记从服务器的值添加新标记并仍然放大位置,我读过的教程如this并没有那么有用。下面是我的MapReady。
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
mMap.setTrafficEnabled(true);
mMap.setIndoorEnabled(true);
mMap.setBuildingsEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
geocoder = new Geocoder(getActivity());
try {
ArrayList<Address> current_adresses = (ArrayList<Address>) geocoder.getFromLocationName(current_location, 1);
for(Address add : current_adresses){
if (current_adresses != null) {//Controls to ensure it is right address such as country etc.
current_longitude = add.getLongitude();
current_latitude = add.getLatitude();
}
}
ArrayList<Address> to_adresses = (ArrayList<Address>) geocoder.getFromLocationName(destination, 1);
for(Address add : to_adresses){
if (to_adresses != null) {//Controls to ensure it is right address such as country etc.
to_longitude = add.getLongitude();
to_latitude = add.getLatitude();
}
}
} catch (IOException e) {
e.printStackTrace();
}
location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
mMap.addMarker(new MarkerOptions()
.position(new LatLng(current_latitude, current_longitude))
.title(first_name + ", " + vehicle)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pickup)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(to_latitude, to_longitude))
.title("Destination")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.office_building)));
CameraPosition cameraPosition= new CameraPosition.Builder()
.target(getCenterCoordinate())
.zoom(13)
.build();
CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(cameraPosition);
mMap.animateCamera(camUpd3);
//AsyncTask to get new address
getTransporterLocation();
/**
*
* I want to set a timer with the AsyncTask inside,
*
* remove the previous marker, convert the new address to lat and long
*
* map the marker and zoom in
*
*/
}
}
答案 0 :(得分:0)
你只需要打电话
mMap.clear();
并重复绘制标记的步骤。