gpt4 book ai didi

android - 谷歌地图安卓 : Live update position of multiple markers

转载 作者:行者123 更新时间:2023-11-29 19:41:36 25 4
gpt4 key购买 nike

我正在尝试创建一张 map ,显示指定人员在 Google map 上的位置。我已经设置了一个带有 MySQL 数据库的 Web 服务器。每当 Android 应用程序运行时,它都会将用户的当前位置存储在数据库中。之后,每当 onLocationChanged() 被调用时,应用程序都会获取用户附近(例如 50 公里)范围内(例如 10 个人)的位置数据地点。使用这些位置坐标,我在 map 上添加了多个标记。现在我面临以下问题:

  1. 当 10 个人的位置数据发生变化时,正在创建新标记而不是原始标记改变其位置。

  2. 当这 10 个人中的任何一个的位置数据从数据库中删除时,相应的标记仍保留在 map 上。

注意:我使用 JSON 从服务器获取数据并在 IntentService 中解析 JSON 数据以创建标记。

这是我解析 JSON 的方式:

@Override
protected void onHandleIntent(Intent intent) {

if (intent != null) {

String json_result = intent.getExtras().getString("JSON Data");
try {
JSONObject jsonObject = new JSONObject(json_result);
JSONArray jsonArray = jsonObject.getJSONArray("location data");

int counter = 0;
String name, lat, lng;
double dist;
int arrayLength;

while (counter < jsonArray.length()) {

JSONObject object = jsonArray.getJSONObject(counter);
name = object.getString("name");
lat = object.getString("lat");
lng = object.getString("lng");
dist = object.getDouble("distance");
arrayLength = jsonArray.length();

Intent jsonDataIntent = new Intent();
jsonDataIntent.setAction(Constants.STRING_ACTION);
jsonDataIntent.putExtra(Constants.STRING_NAME, name);
jsonDataIntent.putExtra(Constants.STRING_LAT, lat);
jsonDataIntent.putExtra(Constants.STRING_LNG, lng);
jsonDataIntent.putExtra(Constants.STRING_DIST, dist);
jsonDataIntent.putExtra(Constants.STRING_LENGTH, arrayLength);
jsonDataIntent.putExtra(Constants.STRING_ID, counter);
LocalBroadcastManager.getInstance(this).sendBroadcast(jsonDataIntent);


counter++;
}

} catch (JSONException e) {
e.printStackTrace();
}

}
}

下面是我如何使用解析的 JSON 数据来创建标记:

private class ParseJSONDataReceiver extends BroadcastReceiver {

Marker usersMarker;
double lat, lng, dist;
int numberOfConnections, id;
HashMap<Marker,Integer> hashMap = new HashMap<>();

@Override
public void onReceive(Context context, Intent intent) {

if (intent != null) {

String name = intent.getExtras().getString(ParseJSONDataService.Constants.STRING_NAME);
lat = Double.parseDouble(intent.getExtras().getString(ParseJSONDataService.Constants.STRING_LAT));
lng = Double.parseDouble(intent.getExtras().getString(ParseJSONDataService.Constants.STRING_LNG));
dist = intent.getExtras().getDouble(ParseJSONDataService.Constants.STRING_DIST);
numberOfConnections = intent.getExtras().getInt(ParseJSONDataService.Constants.STRING_LENGTH);
id = intent.getExtras().getInt(ParseJSONDataService.Constants.STRING_ID) + 1;


usersMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Name: " + name)
.snippet(id + ": Distance: " + dist + " Km")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

hashMap.put(usersMarker,id);

}

}
}

谁能帮我解决以上问题????

最佳答案

要更新现有标记的位置,您需要更改代码,请使用 HashMap<Integer, Marker>而不是 HashMap<Marker, Integer>这样您就可以通过用户 ID 查询您的 map :

HashMap<Integer, Marker> hashMap = new HashMap<>();

然后,您像这样添加我们的标记(更新现有标记):

if (hashMap.containsKey(id)) {
Marker marker = hashMap.get(id);
marker.setPosition(new LatLng(lat, lng)); // Update your marker
} else {
usersMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Name: " + name)
.snippet(id + ": Distance: " + dist + " Km")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

hashMap.put(id, usersMarker);
}

如果你想删除一个用户,你还需要从 map 和 HashMap 中删除它的标记。 :

Marker marker = hashMap.get(id);
if (marker!= null) {
marker.remove();
hashMap.remove(id);
}

关于android - 谷歌地图安卓 : Live update position of multiple markers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38583701/

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