【问题标题】:How to add Custom Views as a marker in Mapbox android?如何在 Mapbox android 中添加自定义视图作为标记?
【发布时间】:2019-12-02 02:49:45
【问题描述】:

所以我想在我的 android 应用程序中添加自定义视图标记,我正在从 API 请求中获取坐标,并且我想在它们各自的标记上显示这些地点的徽标。我从同一个 API 请求中获取标记的 URL。此外,我希望所有标记都成为单个符号图层的一部分,因为我也想对这些标记进行聚类。图片来源:Google 图片搜索。

【问题讨论】:

标签: android kotlin mapbox mapbox-android mapbox-marker


【解决方案1】:

查看https://docs.mapbox.com/android/maps/examples/symbol-layer-info-window。在该示例中,GenerateViewIconTask 使用View 创建BitmapBitmap(s) 被添加到地图中以用作 SymbolLayer 图标。

或者,您可以使用 Android 版 Mapbox 注释插件:https://docs.mapbox.com/android/plugins/overview/annotation/

【讨论】:

  • 我想你没有明白我的意思,我正在使用我想要的注释插件,在符号层中显示每个地方的徽标,就像我正在获取我想要的地方的协调放置标记,这些地方有它们的徽标图像,我想在标记中显示该位置的徽标,例如具有某种标记形状的自定义标记并在其上放置徽标。
  • 啊,现在我明白了,你是说首先我应该为上述示例中的所有图标(如信息窗口)制作一个符号层,对吧??
  • 是的,没错。将自定义视图转换为位图,然后将这些位图传递/使用到插件/地图。
【解决方案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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多