首先,我像这样绘制一个Marker
:
public void addMarker(String title,String lat,String Lng,int id,String address,int f)
{
marker= mMap.addMarker(new MarkerOptions().snippet(title)
.title(title+", "+address)
.position(new LatLng(Double.valueOf(lat), Double.valueOf(Lng)))
.icon(BitmapDescriptorFactory.fromResource(id)));
LatLng coordinate = new LatLng(Double.valueOf(lat), Double.valueOf(Lng));
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 10);
mMap.animateCamera(yourLocation);
mMarkerArray.add(marker);
}
之后,当我到达任何现有的Location
@Override
public void onLocationChanged(Location location)
{
Log.d("latitude_main", "onlocation???");
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.e("latitude_main", "latitude--" + latitude+"longitude="+longitude);
current_lat= String.valueOf(latitude);
current_lng= String.valueOf(longitude);
Log.e("latitude_main","size-=="+salesmanlocationArrayList.size() );
for(int i=0;i<salesmanlocationArrayList.size();i++)
{
if(salesmanlocation.getLati().equals("12.9165757") && salesmanlocation.getLongi().equals("77.6101163"))
{
mMap.addMarker(new MarkerOptions()
.snippet(""+i).title(salesmanlocation.getFirm_name()+", "+salesmanlocation.getAddress())
.position(new LatLng(Double.valueOf(salesmanlocation.getLati().toString()), Double.valueOf(salesmanlocation.getLongi().toString())))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.event_events_select)));
}
mapFragment.getMapAsync(this);
}
}
我想在用户访问该位置时从地图中删除标记。
答案 0 :(得分:1)
您可以简单地定义一个执行任务的OnMyLocationChangeListener
类,并将其设置在GoogleMap
实例上,这样您就可以随时在应用程序中使用它。
第1步 - 定义您的听众
public class MyMarkerLocationListener implements GoogleMap.OnMyLocationChangeListener {
List<Marker> markerList;
int MY_DISTANCE;
GoogleMap mMap;
public MyMarkerLocationListener(List<Marker> markerList, int meters, GoogleMap mMap)
{
this.markerList = markerList;
this.MY_DISTANCE = meters;
this.mMap = mMap;
}
@Override
public void onMyLocationChange(Location location) {
// your code/logic
//...
Location myNewLocation = location;
Location someMarkerLocation = new Location("some location");
//for each marker on your list
//check if you are close to it
for (Marker m : markerList) {
LatLng markerPosition = m.getPosition();
someMarkerLocation.setLatitude(markerPosition.latitude);
someMarkerLocation.setLongitude(markerPosition.longitude);
if (myNewLocation.distanceTo(someMarkerLocation) < MY_DISTANCE) {
//remove marker
m.remove();
//or if you still want to use it later
//m.setVisible(false);
// add your new marker
//mMap.addMarker(new MarkerOptions().icon()....);
}
}
}
}
定义课程后,您只需在地图上设置片段或活动代码上的监听器=)
第2步 - 实现监听器并设置
MyMarkerLocationListener myListener = new MyMarkerLocationListener(mMarkerArray, 100, mMap);
mMap.setOnMyLocationChangeListener(myListener);
更新以在评论中回答您的问题:
你应该在使用它之前初始化mMap,看一下这段代码Stackoverflow question
public class MapPane extends Activity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
//DO WHATEVER YOU WANT WITH GOOGLEMAP
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
}
不要忘记你的活动应该实现OnMapReadyCallback接口,以便调用onMapReady方法
只有在准备就绪后才能使用地图
希望这有帮助!