【问题标题】:Android Google Map Marker info to TextViewAndroid Google Map Marker 信息到 TextView
【发布时间】:2017-09-16 09:59:19
【问题描述】:

我想将我的标记信息发送到 textView,我可以做到,但它总是只显示 JSON 的最后一个,有什么好的想法可以解决这个问题吗?

我的计划很简单,当用户点击标记时,它将使 RealitveLayout“可见”,到目前为止它工作正常,但我的问题是我从 JSON 收集我的标记,然后将它们广告到 MAP,

有一个 OnClick 监听器是什么使伎俩 (设置布局 VISIBLE 并向 TextView 发送该布局上的信息。)

它可以工作,但它只从 JSON 数组中获取最后一个对象。

 public GoogleMap.OnInfoWindowClickListener getInfoWindowClickListener()
{
    return new GoogleMap.OnInfoWindowClickListener()
    {
        @Override
        public void onInfoWindowClick(Marker marker)
        {
            RelativeLayout rl1 = (RelativeLayout) findViewById(R.id.popup); 
            rl1.setVisibility(View.VISIBLE); // Set the View to VISIBLE


            //Title to TextView (also "Score" info)
            TextView pealkiriTextView = (TextView) findViewById(R.id.pealkiriTextView);
            pealkiriTextView.setText(title + " (" + punkte + " punkti)");

            //Description to TextView
            TextView pKirlejdusTextView = (TextView) findViewById(R.id.pKirlejdusTextView);
            pKirlejdusTextView.setText(kirjeldus);

        }
    };}

这就是我从 JSON(来自 URL)获取信息的方式

private void getMarkers() {
    StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.e("Response: ", response.toString());

            try {
                JSONObject jObj = new JSONObject(response);
                String getObject = jObj.getString("punktid");
                JSONArray jsonArray = new JSONArray(getObject);

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    nr = jsonObject.getString(NR);
                    title = jsonObject.getString(TITLE);
                    kirjeldus = jsonObject.getString(KIRJELDUS);
                    vahend = jsonObject.getString(VAHEND);
                    punkte = jsonObject.getString(PUNKTE);
                    latLng = new LatLng(Double.parseDouble(jsonObject.getString(LAT)), Double.parseDouble(jsonObject.getString(LNG)));

                    // Anname addMarkerile väärtused
                    addMarker(latLng, title, kirjeldus,punkte, nr, vahend);
                }
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Error: ", error.getMessage());
            Toast.makeText(kaart.this, error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

并在地图上添加标记:

    private void addMarker(LatLng latlng,final String nr, final String title, final String kirjeldus,final String punkte, final String vahend) {
    markerOptions.position(latlng);
    //markerOptions.title(title +"(" + punkte +"p)");
    markerOptions.title(punkte +" Punkti");
    //markerOptions.snippet(kirjeldus);
    if (vahend.equalsIgnoreCase("auto")) { markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.auto));
    } else { markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.jala)); }
    gMap.addMarker(markerOptions);} //addMarker

【问题讨论】:

    标签: javascript android json textview maps


    【解决方案1】:

    为了在此处获取 Google 地图标记信息,我创建了一个自定义信息窗口

    public class CustomWindowInfo implements GoogleMap.InfoWindowAdapter {
    
    // Use default InfoWindow frame
    @Override
    public View getInfoWindow(Marker arg0) {
        return null;
    }
    
    // Defines the contents of the InfoWindow
    @Override
    public View getInfoContents(Marker arg0) {
    
        // Getting view from the layout file info_window_layout
        View v = getLayoutInflater().inflate(R.layout.custom_map_window, null);
        TextView texttitle = (TextView) v.findViewById(R.id.texttitle);
        TextView text_description = (TextView) v.findViewById(R.id.text_description);
    
        texttitle.setText(arg0.getTitle());
    
        // Setting the location to the textview
        text_description.setText(arg0.getSnippet());
        return v;
    
    }}
    

    为谷歌地图设置自定义信息窗口

    googleMap.setInfoWindowAdapter(new CustomWindowInfo());
    googleMap.setOnInfoWindowClickListener(listenerInfowindow_click);
    

    并在点击时获取标记的信息

    public GoogleMap.OnInfoWindowClickListener listenerInfowindow_click = new GoogleMap.OnInfoWindowClickListener() {
    
            @Override
            public void onInfoWindowClick(final Marker marker) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MapActivity.this);
                builder.setTitle("");
                builder.setMessage(dialog_mas);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                        if (InterNet.isConnectedToInternet()) {
                        Geocoder selected_place_geocoder = new Geocoder(MapActivity.this);
                        address =  selected_place_geocoder.getFromLocationName(marker.getSnippet(), 1);
    
                        } else {
                            Toast.makeText(MapActivity.this,InterNet.interNetMsg, Toast.LENGTH_LONG).show();
                        }
                        dialog.cancel();
                    }
                });
                builder.setNegativeButton("N0", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
    
                    }
                });
                builder.show();
    
    
            }
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-25
      • 2020-03-07
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      相关资源
      最近更新 更多