在谷歌地图V2中创建自定义信息窗口

6

我遇到了创建自己的CustomInfoWindow的问题,却不知道为什么会出现异常。

这是我的简单CustomInfoWindow类:

public class CustomInfoWindow implements InfoWindowAdapter {

private LayoutInflater mInflater;

public CustomInfoWindow(LayoutInflater inflater) {
    this.mInflater=inflater;

}


@Override
public View getInfoContents(Marker marker) {
    View popup = mInflater.inflate(R.layout.info_window_layout, null);
    TextView tv=(TextView)popup.findViewById(R.id.title);
    tv.setText(marker.getTitle());
    tv=(TextView)popup.findViewById(R.id.address);
    tv.setText(marker.getSnippet());

    return popup;
}

@Override
public View getInfoWindow(Marker marker) {
    View popup = mInflater.inflate(R.layout.info_window_layout, null);
    TextView tv=(TextView)popup.findViewById(R.id.title);
    tv.setText(marker.getTitle());
    tv=(TextView)popup.findViewById(R.id.address);
    tv.setText(marker.getSnippet());

    return popup;
}

我在这里设置它。(MainActivity)

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            Statics.LAT_TLV, Statics.LON_TLV), 13));

    CustomInfoWindow customInfoWindow = new CustomInfoWindow(getLayoutInflater());
    mMap.setInfoWindowAdapter(customInfoWindow);

我没有实现onMarkerClick方法。 地图加载正确,标记也正常显示(大约40个),但是当我点击其中一个标记时,出现以下错误:

    01-10 13:15:24.321: E/AndroidRuntime(21162): FATAL EXCEPTION: main
01-10 13:15:24.321: E/AndroidRuntime(21162): java.lang.NullPointerException
01-10 13:15:24.321: E/AndroidRuntime(21162):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at android.view.View.measure(View.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.y.i(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.y.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.w.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.bd.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.y.bw.b(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.y.bw.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.dh.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.n.c(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.dw.a(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.bd.c(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.a.dq.onSingleTapConfirmed(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.e.v.onSingleTapConfirmed(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at maps.e.j.handleMessage(Unknown Source)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at android.os.Handler.dispatchMessage(Handler.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at android.os.Looper.loop(Looper.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at android.app.ActivityThread.main(ActivityThread.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at java.lang.reflect.Method.invoke(Method.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
01-10 13:15:24.321: E/AndroidRuntime(21162):    at dalvik.system.NativeStart.main(Native Method)

希望有人能够帮助我,并且如果有一个非常简单的示例来创建自己的信息视图,那将是非常棒的。非常感谢。Udi


我遇到了完全相同的错误,如果我使用getInfoContents一切正常,但是如果我尝试使用getInfoWindows,应用程序会崩溃。 - Ernest Poletaev
1个回答

13

如果getInfoWindow()永远不会返回null,那么你不应该同时重写getInfoWindow()getInfoContents()。你的getInfoContents()永远不会被使用。

如果我要猜测,你的问题可能出在布局文件中,但这只是一个猜测。

这是一个示例InfoWindowAdapter

class PopupAdapter implements InfoWindowAdapter {
  LayoutInflater inflater=null;

  PopupAdapter(LayoutInflater inflater) {
    this.inflater=inflater;
  }

  @Override
  public View getInfoWindow(Marker marker) {
    return(null);
  }

  @Override
  public View getInfoContents(Marker marker) {
    View popup=inflater.inflate(R.layout.popup, null);

    TextView tv=(TextView)popup.findViewById(R.id.title);

    tv.setText(marker.getTitle());
    tv=(TextView)popup.findViewById(R.id.snippet);
    tv.setText(marker.getSnippet());

    return(popup);
  }
}

使用这个布局文件来为InfoWindow设置布局:

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:padding="2dip"
        android:src="@drawable/ic_launcher"
        android:contentDescription="@string/icon"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/snippet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"/>
    </LinearLayout>

</LinearLayout>

完整的示例应用程序可以在此处找到:https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Popups


这看起来很不错...但是如何从MainActivity调用它并将其应用于地图上的每个标记? - smartmouse
@smartmouse:这适用于地图的每个标记,而PopupAdapter应用于MainActivity,当您检查答案中链接到的项目时,可以看到。 - CommonsWare
你知道如何在聚类中显示每个标记的标题和摘要吗? - smartmouse
我正在使用这个:https://developers.google.com/maps/documentation/android/utility/marker-clustering - smartmouse
在PopupAdapter.java中,当我使用marker.getTitle()和marker.getSnippet()时,它们是空的。如何验证它们? - smartmouse
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接