gpt4 book ai didi

android - 当用户访问该位置(接近标记)时,我想从 map 中替换标记

转载 作者:行者123 更新时间:2023-11-29 01:15:46 25 4
gpt4 key购买 nike

首先,我绘制了一个 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

时,我会尝试用另一个图标替换 Marker
@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);
}
}

我想在用户访问该位置时从 map 上删除标记。

最佳答案

您可以简单地定义一个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()....);
}

}
}
}

定义类后,您只需在 fragment 或 Activity 代码的 map 上设置监听器 =)

第 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);
}
}

不要忘记您的 Activity 应该实现 OnMapReadyCallback 接口(interface)以便调用 onMapReady 方法

map 准备好后才能使用

希望这对您有所帮助!

关于android - 当用户访问该位置(接近标记)时,我想从 map 中替换标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39771352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com