android如何可以添加此自定义信息 window 在Google Maps应用程序而不是默认的?

发布时间: 2017/3/19 17:20:13
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这是代码。我已经创建了自定义 adapter 信息 window ,应该替换默认的其中一个。我已经设置为自定义地图 adapter ,但它仍然显示旧。我是一个在 java 的初学者。所以,它将有助于知道如何修改代码,以添加自定义信息window

MapsActivity

package com.example.nobodyme.buildingareafinder;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapsActivity extends FragmentActivity implementOnMapReadyCallback {

private GoogleMap mMap;
private int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_maps);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map1);
    mapFragment.getMapAsync(this);


}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    //setting up custom info window adapter to map
    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());


    //Long click listener for placing marker on map
    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng latLng) {
            Marker marker = mMap.addMarker(new MarkerOptions()
                    .position(latLng)
                    .visible(true)
                    .draggable(true)
                    .title("Marker " + i)
                    .snippet("Point added " + i));


            marker.setDraggable(true);

            Toast.makeText(MapsActivity.this, latLng.toString(), Toast.LENGTH_LONG).show();
            i++;
        }
    });


    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            marker.remove();
        }
    });


}


// class for custominfoadapter

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private View view;

    //to display heading distance
    private String title_distance = getResources().getString(R.string.distance);

    //to display distance from other markers
    private String distance_no;

    //to display snippet from original adpater
    private String custom_snippet;

    public CustomInfoWindowAdapter() {
        view = getLayoutInflater().inflate(R.layout.custom_info_window,
                null);
    }

    @Override
    public View getInfoContents(Marker marker) {

        distance_no = marker.getTitle();
        custom_snippet = marker.getSnippet();

        return null;
    }


    @Override
    public View getInfoWindow(final Marker marker) {


        return null;
    }


}

}

custom_info_window.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:background="@android:color/background_light"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ff0000"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/snippet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ff7f7f7f"
        android:textSize="14dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_delete" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/custom_marker_remove"
            android:gravity="center"
            android:textColor="#ff7f7f7f"
            android:textSize="14dp"
            android:layout_alignParentTop="true" />
    </LinearLayout>


</LinearLayout>

解决方法 1:

你正在返回 nullgetInfoWindow 的方法你 CustomInfoWindowAdapter

这里是一个工作实现︰

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public View getInfoWindow(final Marker marker) {
        View v = getLayoutInflater().inflate(R.layout.custom_info_window, null);

        TextView title = (TextView)v.findViewById(R.id.title);
        title.setText(marker.getTitle());

        TextView snippet = (TextView)v.findViewById(R.id.snippet);
        snippet.setText(marker.getSnippet());

        return v;
    }
}

考虑到 account ,根据文档

信息 window 就是画就是不活 viewview呈现为图像 (使用 View.draw ( Canvas )) 在 time 它返回。这意味着,对任何后续更改 view 将不会反映信息由 window 在地图上。若要更新信息 window 后后 (例如,已加载图像),调用 showInfoWindow()。此外,信息 window 不会尊重任何典型的一种正常的互动 viewtouchgesture 事件。不过你可以听一听一个泛型 click 事件在整个信息 window 下面一节中所述。

这意味着, Button s 或可点击 View s 添加到信息窗口将不工作。

官方微信
官方QQ群
31647020