2015-01-07 65 views
2

我目前正在分析包含了坐标和HTML代码中JSON对象,并在谷歌地图Android版显示他们......到目前为止,我有这样的方法:Android的谷歌地图HTML标记中的信息窗口

private void createMarkersFromJson(String json) throws JSONException { 
// De-serialize the JSON string into an array of objects 
    JSONArray jsonArray = new JSONArray(json); 
    for (int i = 0; i < jsonArray.length(); i++) { 
// Create a marker for each object in the JSON data. 
     JSONObject jsonObj = jsonArray.getJSONObject(i); 
     mMap.addMarker(new MarkerOptions() 
         .position(new LatLng(
           jsonObj.getDouble("lat"), 
           jsonObj.getDouble("long") 
         )) 
     ); 
    } 
} 

参考对于以上代码:https://gist.github.com/TimPim/5902100

此方法成功显示标记。 让我们说我的JSON对象的一部分,我有一个名为“contentString”,其中包含HTML代码的记录。我在想,我怎么会能够在信息窗口中正确显示这个像谷歌显示这里它的JavaScript API:

https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple

我已经看过这个答案,并试图实现它自己,但没有成功: Android Google Maps V2 Title or Snippets Strings From HTML

如果有人能告诉我一个更详细的解决方案,以及它如何应用于这个领域,它将不胜感激。

回答

2

想通了。编辑看起来像这样的setUpMapIfNeeded方法:

private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 
      mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 
       @Override 
       public View getInfoWindow(Marker marker) { 
        return null; 
       } 

       @Override 
       public View getInfoContents(Marker marker) { 
        View v = getLayoutInflater().inflate(R.layout.infowindow_layout, null); 
        TextView textView = (TextView) v.findViewById(R.id.marker_label); 
        Spanned spannedContent = Html.fromHtml(marker.getTitle()); 
        textView.setText(spannedContent, TextView.BufferType.SPANNABLE); 
        return v; 
       } 
      }); 
      setUpMap(); 
     } 
    } 
} 

凡marker.getTitle(),获得标题从标记中的其他方法(包含我们的HTML内容),并设置在我们新创建的信息窗口的文本。本教程是非常有益的:https://www.youtube.com/watch?v=g7rvqxn8SLg

我还创建它看起来像这样高分辨率下的infowindow_layout.xml /布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
    android:id="@+id/station_info_layout" 
    android:layout_width="205dp" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/marker_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/marker_label" 
     android:layout_marginLeft="5dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 


</LinearLayout> 

如果有人想我澄清我的回答,请让我知道,我将编辑这篇文章。

0

正如我所见,您可以使用Android的Custom info windows达到目标。

为此,您必须创建InfoWindowAdapter接口的具体实现,然后在您的实现中调用GoogleMap.setInfoWindowAdapter()。该接口包含两个方法供您执行:getInfoWindow(Marker)getInfoContents(Marker). API将首先调用getInfoWindow(Marker),如果返回null,则它将调用getInfoContents(Marker)。如果这也返回null,那么默认的信息窗口将被使用。

详情请参阅here

相关问题