自定义地图信息窗口适配器,带有自定义数据在地图V2中。

45

我想在安卓地图v2中制作自定义信息窗口适配器,就像下面这样。

enter image description here

我看到了下面的链接,但是没有更多信息。

1,2,3,

下面是我的内容布局文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
>
    <ImageView
        android:id="@+id/infocontent_iv_image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentTop="true" 
    />
    <RelativeLayout
        android:id="@+id/infocontent_rl_middle"
        android:layout_below="@id/infocontent_iv_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_margin="5dp"
    >

    </RelativeLayout>
    <TextView
        android:id="@+id/infocontent_tv_name"
        android:layout_below="@id/infocontent_rl_middle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" 
        android:layout_margin="5dp"
    />
    <TextView
        android:id="@+id/infocontent_tv_type"
        android:layout_below="@id/infocontent_tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#CCCCCC"
        android:layout_margin="5dp"
    />
    <TextView
        android:id="@+id/infocontent_tv_desc"
        android:layout_below="@id/infocontent_tv_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
    />
    <TextView
        android:id="@+id/infocontent_tv_addr"
        android:layout_below="@id/infocontent_tv_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
    />
</RelativeLayout>

有人能帮我吗,如何在infowindow适配器中设置数据到所有的视图中?


只需在“X”按钮单击时使用“dialog.dismiss”,它也会在设备的返回按钮上关闭。 - Girish Bhutiya
嗨,Girish!我也想像你一样做。你的答案能否稍微解释一下或提供一些参考资料? - TheLittleNaruto
2个回答

73

试试这个

windowlayout.xml

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

    <TextView
        android:id="@+id/tv_lat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_lng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

public class MainActivity extends FragmentActivity {


GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting reference to the SupportMapFragment of activity_main.xml
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting GoogleMap object from the fragment
    googleMap = mapFragment.getMap();

    // Setting a custom info window adapter for the google map
    googleMap.setInfoWindowAdapter(new 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.windowlayout, null);

            // Getting the position from the marker
            LatLng latLng = arg0.getPosition();

            // Getting reference to the TextView to set latitude
            TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);

            // Getting reference to the TextView to set longitude
            TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);

            // Setting the latitude
            tvLat.setText("Latitude:" + latLng.latitude);

            // Setting the longitude
            tvLng.setText("Longitude:"+ latLng.longitude);

            // Returning the view containing InfoWindow contents
            return v;

        }
    });

    // Adding and showing marker while touching the GoogleMap
    googleMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng arg0) {
            // Clears any existing markers from the GoogleMap
            googleMap.clear();

            // Creating an instance of MarkerOptions to set position
            MarkerOptions markerOptions = new MarkerOptions();

            // Setting position on the MarkerOptions
            markerOptions.position(arg0);

            // Animating to the currently touched position
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(arg0));

            // Adding marker on the GoogleMap
            Marker marker = googleMap.addMarker(markerOptions);

            // Showing InfoWindow on the GoogleMap
            marker.showInfoWindow();

        }
    });

}
}

通过这种方式,您可以创建自定义布局并实现您想要的方式。

如果有帮助,请告诉我。


你说的“所有视图”是什么意思?视图只有一个,数据会不同吗? - moDev
1
只需要应用逻辑。取一个循环并添加标记以及位置,同样的方式显示内容。 - moDev
1
看这个答案:https://dev59.com/MGYr5IYBdhLWcg3wYpUg - LKallipo
完美的决定!谢谢! - Konstantin.Efimenko
@CodeWord 请查看此链接 https://developers.google.com/maps/documentation/android/marker#marker_click_events - moDev
显示剩余13条评论

22

我在onMarker()点击时打开对话框,并使用setContentView(my layout)

我使用以下代码。

@Override
    public boolean onMarkerClick(Marker arg0) {
        if(arg0.getSnippet() == null){
            mMap.moveCamera(CameraUpdateFactory.zoomIn());
            return true;
        }
        //arg0.showInfoWindow();
        final DataClass data = myMapData.get(arg0);
        final Dialog d = new Dialog(MainActivity.this);
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //d.setTitle("Select");
        d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        d.setContentView(R.layout.info_content);
        ivPhoto = (ImageView)d.findViewById(R.id.infocontent_iv_image);
        AddImageOnWindow executeDownload = new AddImageOnWindow();
        final LatLng l = arg0.getPosition();
        executeDownload.execute(l);
        TextView tvName = (TextView)d.findViewById(R.id.infocontent_tv_name);
        tvName.setText(data.getPlaceName());

        TextView tvType = (TextView)d.findViewById(R.id.infocontent_tv_type);
        tvType.setText("("+data.getPlaceType()+")");

        TextView tvDesc = (TextView)d.findViewById(R.id.infocontent_tv_desc);
        tvDesc.setText(data.getPlaceDesc());

        TextView tvAddr = (TextView)d.findViewById(R.id.infocontent_tv_addr);
        tvAddr.setText(Html.fromHtml(data.getPlaceAddr()));

d.show();
return true;

2
你能详细解释一下你的回答吗? - Sheharyar
2
@GirishBhutiya,你怎么让对话框指向标记的顶部?(而不是随意弹出),谢谢。 - EyeQ Tech
1
我更喜欢这个解决方案,因为它可以完全控制外观。此外,您可以添加按钮和其他控件。我认为屏幕位置并不那么重要:对话框无论如何都会覆盖大部分屏幕,并且用户知道他点击了哪里。 - user2461595
@NewBee 这只是从互联网上下载图片,没有其他功能。 - Girish Bhutiya
这是一个很好的解决方案,但我想在标记上方显示对话框。这可行吗? - Hampel Előd
显示剩余3条评论

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