所以我想在我的 android 应用程序中添加自定义视图标记,我从 API 请求中获取坐标,并且我想在它们各自的标记上显示这些地方的徽标。我从同一个 API 请求中获取标记的 URL。此外,我希望所有标记都成为单个符号图层的一部分,因为我也想对这些标记进行聚类。图片来源:谷歌图片搜索。
问问题
2980 次
2 回答
2
我已经解决了这个问题。
首先,您必须创建一个私有类,该类将为您获取数据,然后将其存储在数组中。
private class getgeo extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Poi_View.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("data");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject JO = (JSONObject) contacts.get(i);
/* if(JO.has("devices"))
{
Toast.makeText(Index.this,"No Data Found",Toast.LENGTH_LONG).show();
}*/
JSONObject jb = (JSONObject) JO.get("ro");
String name = jb.getString("name");
String center = jb.getString("center");
HashMap<String, String> contact = new HashMap<>();
contact.put("name", name);
contact.put("center", center);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Your Internet Connection is Weak. Try Reloading Your Page",
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Check Your Internet Connection",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
之后,您必须在 mapbox onStyleloaded() 方法中执行它
public void onStyleLoaded(@NonNull Style style) {
for(int a=0;a<contactList.size();a++)
{
HashMap<String, String> hashmap = contactList.get(a);
pump_name= hashmap.get("name");
pump_center= hashmap.get("center");
String fullname = pump_center;
String[] names = fullname.split(",", 3); // "1" means stop splitting after one space
String firstName = names[0];
String lastName = names[1];
double lat = Double.parseDouble(firstName);
double lng = Double.parseDouble(lastName);
System.out.println(lat);
System.out.println(pump_name);
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title(pump_name));
//onMapReady(mapboxMap);
}
}
于 2020-01-20T05:45:20.870 回答
0
查看https://docs.mapbox.com/android/maps/examples/symbol-layer-info-window。GenerateViewIconTask
在该示例中,使用 aView
创建一个Bitmap
. ( Bitmap
s) 添加到地图中以用作SymbolLayer
图标。
或者,您可以使用 Android 版 Mapbox 注释插件:https ://docs.mapbox.com/android/plugins/overview/annotation/
于 2019-07-24T05:38:08.167 回答