2017-09-16 74 views
0

我想让我的标记信息textView,我能够做到这一点,但它始终只显示从JSON的最后一个,任何好主意如何解决?安卓谷歌地图标记信息到TextView

我的计划是很容易,当标记用户点击话反而会使得RealitveLayout“可见”,它的工作原理,到目前为止,但我的问题是,我收集我的标记从JSON,然后广告他们的MAP,

有听者的OnClick什么使的伎俩 (设置布局可见和发送信息给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 

回答

0

为了让谷歌地图标记信息在这里,我创建了一个自定义信息窗口

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

,并获得标志的inforamtion上点击

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(); 


     } 
    };