美文网首页AndroidAndroidAndroid Tips
三步实现地图自定义InfoWindow

三步实现地图自定义InfoWindow

作者: Teprinciple | 来源:发表于2016-11-11 13:57 被阅读7789次
项目地址:https://github.com/teprinciple/AMapInfoWindowDemo

</br>

前言

现在的app很多都需要接入地图,不同的app都会定义自己的marker以及InforWindow,本文以高德地图为例,描述自己自定义InfoWindow之路。
先看看效果图:


高德地图的SDK以及接入流程,官方文档已经写的很明白了:http://lbs.amap.com/

第一步:自定义InforWindow的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="127dp"
    android:background="#0000"  
    >
    <LinearLayout
        android:layout_width="209dp"
        android:layout_height="127dp"
        android:orientation="vertical"
        android:background="@drawable/inforwindow_bg">
        <TextView
            android:id="@+id/agent_name"
            android:layout_marginLeft="14dp"
            android:layout_marginRight="14dp"
            android:layout_marginTop="11dp"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:textSize="14sp"
            android:textColor="@color/black_text"/>
        <TextView
            android:id="@+id/agent_addr"
            android:layout_marginLeft="14dp"
            android:layout_marginRight="14dp"
            android:layout_marginTop="2dp"
            android:layout_width="match_parent"
            android:layout_height="17dp"
            android:singleLine="true"
            android:textSize="12sp"
            android:textColor="@color/black_text2"/>
        <TextView
            android:id="@+id/waitNum"
            android:layout_marginLeft="14dp"
            android:layout_marginTop="2dp"
            android:layout_width="wrap_content"
            android:layout_height="17dp"
            android:textSize="12sp"
            android:textColor="#1DA1F2"/>
        <View
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="9dp"
            android:background="#DFDFDF"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_marginTop="9dp"
                android:id="@+id/navigation_LL"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                >
                <ImageView
                    android:layout_marginLeft="27dp"
                    android:layout_width="17dp"
                    android:layout_height="18dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/inforwindow_navigation"/>
                <TextView
                    android:layout_marginLeft="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/infowindow_navigation"
                    android:textSize="14sp"
                    android:textColor="@color/black_text"/>
            </LinearLayout>
            <View
                android:layout_width="1dp"
                android:layout_height="30dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="13dp"
                android:background="#DFDFDF"/>
            <LinearLayout
                android:layout_marginTop="9dp"
                android:id="@+id/call_LL"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                >
                <ImageView
                    android:layout_marginLeft="23dp"
                    android:layout_width="19dp"
                    android:layout_height="18dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/inforwindow_call"/>
                <TextView
                    android:layout_marginLeft="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/infowindow_call"
                    android:textSize="14sp"
                    android:textColor="@color/black_text"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <!-- 用来偏移inforwindow的位置-->
    <TextView
        android:layout_width="38dp"
        android:layout_height="match_parent" />
</LinearLayout>

第二步、自定义适配器

需要继承高德地图的AMap.InfoWindowAdapter

package teprinciple.yang.amapinforwindowdemo.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;

import teprinciple.yang.amapinforwindowdemo.base.BaseApplication;
import teprinciple.yang.amapinforwindowdemo.R;
import teprinciple.yang.amapinforwindowdemo.utils.NavigationUtils;
import teprinciple.yang.amapinforwindowdemo.utils.PhoneCallUtils;


/**
 * Created by Teprinciple on 2016/8/23.
 * 地图上自定义的infowindow的适配器
 */
public class InfoWinAdapter implements AMap.InfoWindowAdapter, View.OnClickListener {
    private Context mContext = BaseApplication.getIntance().getBaseContext();;
    private LatLng latLng;
    private LinearLayout call;
    private LinearLayout navigation;
    private TextView nameTV;
    private String agentName;
    private TextView addrTV;
    private String snippet;

    @Override
    public View getInfoWindow(Marker marker) {
        initData(marker);
        View view = initView();
        return view;
    }
    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    private void initData(Marker marker) {
        latLng = marker.getPosition();
        snippet = marker.getSnippet();
        agentName = marker.getTitle();
    }

    @NonNull
    private View initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_infowindow, null);
        navigation = (LinearLayout) view.findViewById(R.id.navigation_LL);
        call = (LinearLayout) view.findViewById(R.id.call_LL);
        nameTV = (TextView) view.findViewById(R.id.name);
        addrTV = (TextView) view.findViewById(R.id.addr);

        nameTV.setText(agentName);
        addrTV.setText(String.format(mContext.getString(R.string.agent_addr),snippet));

        navigation.setOnClickListener(this);
        call.setOnClickListener(this);
        return view;
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.navigation_LL:  //点击导航
                NavigationUtils.Navigation(latLng);
                break;

            case R.id.call_LL:  //点击打电话
                PhoneCallUtils.call("028-"); //TODO 处理电话号码
                break;
        }
    }
}

第三步、调用

package teprinciple.yang.amapinforwindowdemo;

import android.os.Bundle;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;

import teprinciple.yang.amapinforwindowdemo.adapter.InfoWinAdapter;
import teprinciple.yang.amapinforwindowdemo.base.BaseActivity;
import teprinciple.yang.amapinforwindowdemo.entity.Constant;
import teprinciple.yang.amapinforwindowdemo.utils.CheckPermissionsActivity;

public class MainActivity extends CheckPermissionsActivity implements AMap.OnMapClickListener, AMap.OnMarkerClickListener {

    private MapView mapView;
    private AMap aMap;
    private UiSettings uiSettings;
    private InfoWinAdapter adapter;
    private Marker oldMarker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //在执行onCreateView时执行mMapView.onCreate(savedInstanceState),实现地图生命周期管理
        mapView.onCreate(savedInstanceState);
        initOperation();
    }

    private void initView() {
        mapView = (MapView) initV(R.id.mapView);
    }

    private void initOperation() {
        initMap();
    }

    /**
     * 初始化地图
     */
    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
            uiSettings = aMap.getUiSettings();
            aMap.setOnMapClickListener(this);
        }
        uiSettings.setZoomControlsEnabled(false); //隐藏缩放控件
        //自定义InfoWindow
        aMap.setOnMarkerClickListener(this);
        adapter = new InfoWinAdapter();
        aMap.setInfoWindowAdapter(adapter);

        addMarkerToMap(Constant.CHENGDU,"成都","中国四川省成都市");
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume(); //管理地图的生命周期
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause(); //管理地图的生命周期
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy(); //管理地图的生命周期
    }

    //地图的点击事件
    @Override
    public void onMapClick(LatLng latLng) {
        //点击地图上没marker 的地方,隐藏inforwindow
        if (oldMarker != null) {
            oldMarker.hideInfoWindow();
            oldMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.marker_normal));
        }
    }

    //maker的点击事件
    @Override
    public boolean onMarkerClick(Marker marker) {
        if (oldMarker != null) {
            oldMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.marker_normal));
        }
        oldMarker = marker;
        marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.marker_selected));
        return false; //返回 “false”,除定义的操作之外,默认操作也将会被执行
    }

    private void addMarkerToMap(LatLng latLng, String title, String snippet) {
        aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
                .position(latLng)
                .title(title)
                .snippet(snippet)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_normal))
        );
    }
}

特别注意

显示infowindow必须设置title或者sinnipet,同时设置aMap.setInfoWindowAdapter(this)

后记

项目地址:https://github.com/teprinciple/AMapInfoWindowDemo
项目中还包括以下功能(均已适配6.0):
1、跳转到高德地图进行导航功能
2、定位功能

相关文章

网友评论

  • Jafir:你好,请问 我自定义的infowindow,为什么宽度只有那么一点。
    xml里面设置了 matchparent或者固定宽度 都不行
  • Mr__小二:老铁,多个同时显示的问题,有办法解决吗
    幷柏然:你好 请问你解决了吗
  • Hk_吼先生:你好 , 问你个问题 :
    你有没有弄个多个Marker的时候 , 你点击第一个出现 InfoWindow 但你点击第二个以后或者第二个都没有出现过 Infowindow , 我在网上看了 说我没有给Marker设置 title,但并不会解决我这个问题,我这边用 的是高德地图。
    Hk_吼先生:@Mr小二 没有弄了现在。请指教。
    Mr__小二:大神,这个问题你解决了吗
  • 拖着行李的蜗牛:你好,请教一下高德在maker上批量添加infowindow,是一个怎样的操作呢,能不能提供点思路
    一步两步三步:单个添加会吗,添加多个要循环添加
  • 一步两步三步:老铁,请问刚进来 InfoWindow默认显示,怎么搞
    TonyEasy:marker.showInfoWindow()试试呢?
  • 菜鸟考官:InfoWindow 可以同时显示几个? 我怎么就只能显示一个呢
    Mr__小二:@菜鸟考官 :+1: 老铁就是6666666
    菜鸟考官:@Mr小二 解决了,我简书里有
    Mr__小二:大神,同时显示的问题你解决了吗
  • yuLiangC:大佬好!我代码写的几乎和你一模一样,但是infowindow里面的点击事件总是没有反应,你写的时候有碰到过这种情况吗?
    Teprinciple: @b4f7c271689a 你添加marker的时候确认一下title或者snippet一定有值
  • 程序亦非猿580230:自定义以后 小蓝点怎么也可以点击了???
    程序亦非猿580230:判断成功之后还是弹出了InfoWindow
    程序亦非猿580230:@Teprinciple 怎么判断是小蓝点??
    Teprinciple: @程序亦非猿580230 加判断,是小蓝点就不响应
  • 一只文艺猿猿猿:您好,这个问题困扰好几天了,麻烦给讲解一下,就是类似您的案例,每个坐标对应的电话是不一样的,如何获取这个坐标点对应的电话号码呢?当然实际项目中还有好几个字段要显示,高德只有,title,和附加信息,这两个字段添加到marker里面,现在我想在marker里面多添加几个字段,类似marker添加自定义字段。。。。您有比较好的方法吗?
    一只文艺猿猿猿: @一名文艺猿 您说的很明白,一点就通了,自己憋了好长时间都没想出来,十分感谢您!
    Teprinciple: @一名文艺猿 其实解决办法有很多,这里提货一种思路。把你所有要现实的信息,用 = 或者其它你信息里面没有的符号拼接起来,作为一个整体作为附加信息,然后在adpter中,再以=把附加信息炸开。你会得到你所有信息的一个数组。这样应该能解决你很多信息要显示的困扰。对于每个marker电话不同,你可以在adpter里面对电话点击等操作设置监听事件;也可以在对应marker被点击时获取到附加信息,再从附加信息中获取到对应的电话号码
  • m厨子:请问下private Context mContext = BaseApplication.getIntance().getBaseContext();
    主界面是fragment的,那上面这段怎么修改呢!
    Teprinciple:你可以通过构造方法传入context 赋值给mContext 。
  • smartfan:Teprinciple老师,你好,怎么设置indowindow显示的位置?我想让点击mark后,infowindow显示在中间
    勇敢说爱:@请别触碰我的梦 @Teprinciple 可以控制显示位置吗?
    smartfan:@Teprinciple 屏幕中间,点击的时候只是mark显示在了中间,想把弹窗设置在中间显示,效果比较好
    Teprinciple:你指的中间是屏幕中间?还是哪里
  • eac9c509c342:你好 请问private Context mContext = BaseApplication.getIntance().getBaseContext();这句代码获取的context的值跟this一样嘛
    Teprinciple: @N7Y 如果你需要一个dialog或者类似的弹窗,是必须要一个Activity的上下文对象才行的
    eac9c509c342:@Teprinciple 我按照您的步骤 后台log可以测试到适配器和onmapclick等方法里面的测试语句 但是窗口无法显示 我在想是不是上面这句代码的问题
    Teprinciple: @N7Y 就是获取一个全局的上下文对象

本文标题:三步实现地图自定义InfoWindow

本文链接:https://www.haomeiwen.com/subject/qzhfpttx.html