Android高德自定义marker点标记与infoWindow窗体

自定义marker与infoWindow窗体

 

 

1.展示地图布局的xml

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

    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

2.Activity自定义marker与infowindow(代码注释详细)

 

/**
 * Created by YyyyQ on 2020/3/24
 */
public class MainActivity extends AppCompatActivity implements AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnMapClickListener {

    private MapView mapView = null;
    private AMap aMap;
    private UiSettings uiSettings;
    //存放所有点的经纬度
    LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
    //当前点击的marker
    private Marker clickMaker;
    //自定义窗体
    View infoWindow = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mapView.getMap();
            uiSettings = aMap.getUiSettings();
            //设置地图属性
            setMapAttribute();
        }

        //模拟数据源
        List<Map<String, String>> list = getData();

        //循坏在地图上添加自定义marker
        for (int i = 0; i < list.size(); i++) {
            LatLng latLng = new LatLng(Double.parseDouble(list.get(i).get("latitude")), Double.parseDouble(list.get(i).get("longitude")));
            MarkerOptions markerOption = new MarkerOptions();
            markerOption.position(latLng);
            markerOption.title(list.get(i).get("title"));
            markerOption.snippet(list.get(i).get("content"));
            //自定义点标记的icon图标为drawable文件下图片
            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ditu_yiliao)));
            markerOption.draggable(false);
            aMap.addMarker(markerOption);
            //将所有marker经纬度include到boundsBuilder中
            boundsBuilder.include(latLng);
        }
        //更新地图状态
        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(boundsBuilder.build(), 10));
    }


    /**
     * 设置地图属性
     */
    private void setMapAttribute() {
        //设置默认缩放级别
        aMap.moveCamera(CameraUpdateFactory.zoomTo(12));
        //隐藏的右下角缩放按钮
        uiSettings.setZoomControlsEnabled(false);
        //设置marker点击事件监听
        aMap.setOnMarkerClickListener(this);
        //设置自定义信息窗口
        aMap.setInfoWindowAdapter(this);
        //设置地图点击事件监听
        aMap.setOnMapClickListener(this);
    }

    /**
     * 模拟数据源
     */
    private List<Map<String, String>> getData() {
        List<Map<String, String>> list = new ArrayList<>();
        for (int i = 1; i < 11; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("title", "标题NO." + i);
            map.put("content", "这是第" + i + "个marker的内容");
            map.put("latitude", 43 + Math.random() + "");
            map.put("longitude", 125 + Math.random() + "");
            list.add(map);
        }
        return list;
    }

    /**
     * marker点击事件
     */
    @Override
    public boolean onMarkerClick(Marker marker) {
        clickMaker = marker;
        //点击当前marker展示自定义窗体
        marker.showInfoWindow();
        //返回true 表示接口已响应事,无需继续传递
        return true;
    }

    /**
     * 监听自定义窗口infoWindow事件回调
     */
    @Override
    public View getInfoWindow(Marker marker) {
        if (infoWindow == null) {
            infoWindow = LayoutInflater.from(this).inflate(R.layout.amap_info_window, null);
        }
        render(marker, infoWindow);
        return infoWindow;
    }

    /**
     * 自定义infoWindow窗口
     */
    private void render(Marker marker, View infoWindow) {
        TextView title = infoWindow.findViewById(R.id.info_window_title);
        TextView content = infoWindow.findViewById(R.id.info_window_content);
        title.setText(marker.getTitle());
        content.setText(marker.getSnippet());
    }

    /**
     * 不能修改整个InfoWindow的背景和边框,返回null
     */
    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    /**
     * 地图点击事件
     * 点击地图区域让当前展示的窗体隐藏
     */
    @Override
    public void onMapClick(LatLng latLng) {
        //判断当前marker信息窗口是否显示
        if (clickMaker != null && clickMaker.isInfoWindowShown()) {
            clickMaker.hideInfoWindow();
        }

    }
}

 

3.自定义infoWindow窗体布局

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/infowindow"
    android:orientation="vertical">
    <!--@drawable/infowindow为.9图-->

    <TextView
        android:id="@+id/info_window_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:textColor="#333"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/info_window_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:textColor="#333"
        android:textSize="12sp" />

</LinearLayout>

 

Android高德自定义marker点标记与infoWindow窗体

全文结束