前言

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

先看看效果图:

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

第一步:自定义InforWindow的布局

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="127dp"

android:background="#0000"

>

android:layout_width="209dp"

android:layout_height="127dp"

android:orientation="vertical"

android:background="@drawable/inforwindow_bg">

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"/>

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"/>

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"/>

android:layout_marginLeft="6dp"

android:layout_marginRight="6dp"

android:layout_width="match_parent"

android:layout_height="1dp"

android:layout_marginTop="9dp"

android:background="#DFDFDF"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_marginTop="9dp"

android:id="@+id/navigation_LL"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

>

android:layout_marginLeft="27dp"

android:layout_width="17dp"

android:layout_height="18dp"

android:scaleType="centerCrop"

android:src="@drawable/inforwindow_navigation"/>

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"/>

android:layout_width="1dp"

android:layout_height="30dp"

android:layout_marginTop="5dp"

android:layout_marginBottom="13dp"

android:background="#DFDFDF"/>

android:layout_marginTop="9dp"

android:id="@+id/call_LL"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

>

android:layout_marginLeft="23dp"

android:layout_width="19dp"

android:layout_height="18dp"

android:scaleType="centerCrop"

android:src="@drawable/inforwindow_call"/>

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"/>

android:layout_width="38dp"

android:layout_height="match_parent" />

第二步、自定义适配器

需要继承高德地图的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、定位功能

bminfowindow是什么_三步实现地图自定义InfoWindow相关推荐

  1. mysql降低数据库版本_三步10分钟搞定数据库版本的降迁 (将后台数据库SQL2008R2降为SQ...

    三步10分钟搞定数据库版本的降迁 (将SQL2008R2降为SQL2005版本) 前思后想仍觉得实战数据库版本的降迁一文中的方式不仅老土而且低效,故有了下文三步搞定数据库从MSSQL2008R2 高版 ...

  2. python输出结果存到文件拒绝访问_三步解决python PermissionError: [WinError 5]拒绝访问的情况...

    问题描述: 1.当你卸载python库的时候,如:pip uninstall pandas 2.当你想要更新python库的时候,如:pip install --upgrade pandas 等等- ...

  3. 微信机器人 java 源码_三步轻松打造微信聊天机器人(附源码)

    最近微信公众平台开发是热门,我也跟风做了一个陪聊的公众号. 其实类似的自动回话程序早就有了,比如前一阵很火的小黄鸡(还是小黄鸭来着?).但尽管是跟风,也要体现一些不同.别人做的都是中文陪聊,咱就来做个 ...

  4. 训练测试数据大小不一致_三步学会训练狗狗不随地大小便

    训练狗狗在规定的地点大小便是非常重要的训练,它决定了你的屋子和院子能否干净整洁.如果是室内训练,我要先告诉你一些相关的训练禁忌. 首先,当狗狗在家里排便之后才对狗狗做出惩罚,是最普遍的一个训练错误,这 ...

  5. omnigraffle 画曲线_三步学会用绘图利器 OmniGraffle 绘制流程图

    原标题:三步学会用绘图利器 OmniGraffle 绘制流程图 OmniGraffle 是 Mac 上的绘图利器,曾获2002年苹果设计奖.Graffle 在很多方面对标 Windows 系统上的 M ...

  6. ebay注册流程_三步教你完成ebay个人店铺的注册

    大家都知道,如果想要做跨境电商,作为服务全球的ebay平台就是最佳选择,对于第一次使用ebay平台的卖家,肯定是要先注册账号,这样才算你拥有使用权. 今天小编就像大家给大家详细分解一下步骤,个人店铺该 ...

  7. java 自定义注解_两步实现Java自定义注解

    什么是注解? 注解就是为容器提供元数据,例如@Controller 注解则是标记了该Bean需要交给Spring容器进行管理. 那么我们怎么去实现自个的注解,也就是自定义注解呢? 一.自定义一个注解( ...

  8. 制作一个查询信息程序_三步学会制作一个小程序

    小程序在刚出现的时候简直是惊艳了整个业界,不管什么内容,光是那酷炫的交互效果就足以篇篇刷屏了,再加上"用完即走",不用安装.不占用内存.入口众多等特性.不过早期由大厂商的专业程序员 ...

  9. python 如何判断一个函数执行完成_三步搞定 Python 中的文件操作

    当程序运行时,变量是保存数据的好方法,但变量.序列以及对象中存储的数据是暂时的,程序结束后就会丢失,如果希望程序结束后数据仍然保持,就需要将数据保存到文件中. Python 提供了内置的文件对象,以及 ...

最新文章

  1. HDU 4609 3-idiots FFT
  2. redis有几种数据类型
  3. linux php 编译 pdo,Linux 下 PHP 扩展 PDO 编译安装
  4. 普通人也要学python吗-普通人学Python有意义吗?
  5. 一个半路出家的前端工程师的2018 | 掘金年度征文
  6. Testing Round #16 (Unrated) C. Skier(map的应用)
  7. 【Python爬虫】Re(正则表达式)库入门
  8. 《Python编程从入门到实践》第10章文件和异常动手试一试答案(附代码)
  9. postfix邮件服务器
  10. 解决Spring Boot 拦截器注入service为空的问题
  11. 2017 ACM-ICPC南宁网络赛: J. Minimum Distance in a Star Graph(BFS)
  12. 常用正则表达式归类贴
  13. 裴礼文数学分析中的典型问题与方法第1章一元函数极限练习
  14. excle表格导出到本地
  15. 现代薄膜温室大棚五大优点,常用的经济作物都有哪些?
  16. 【LuoguP2466】[SDOI2008] Sue的小球
  17. matlab fspecial用法,matlab fspecial 用法解释
  18. 高中会考 计算机网络技术考试,2020年高中信息技术会考真题5附带答案已排版可打印共计20套...
  19. php 请求url并且获取返回值
  20. 环境问题:fatal error LNK1318: 非意外的 PDB 错误

热门文章

  1. 实例:加载图片并添加文本水印
  2. 如何使用iPad移动办公
  3. Linux搭建lamp(Apache+PHP+Mysql环境)centos7.2版详细教程
  4. 算法学习之路|链表元素分类
  5. iftop 监控linux服务器网卡流量
  6. 《解释的工具:生活中的经济学原理 读书笔记6》
  7. hdu 4350 2012 多校 - 6
  8. 学习:erlang的term反序列化,string转换为term
  9. 如何根据指定软件版本制作属于自己的puppet yum源
  10. 首次吃了一颗带奶糖味的消炎药,不知道管用不