前言:在android开发过程中,百度地图的使用是比较普遍的,但是如何使用,使用什么版本的百度API还是需要一些讲究。

在项目过程中,需要用到百度地图的marker和InfoWindow的功能。

标注覆盖物(百度地图官方图)

布局文件很简单,主要就是mapview,如下:

android:layout_width="match_parent" android:layout_height="match_parent"

android:orientation="vertical"

android:background="@color/overall_bg">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:id="@+id/gps_button_reset"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/gps_reset_button"

android:layout_marginBottom="8dp"

android:layout_marginLeft="16dp"

android:layout_marginTop="8dp"

android:layout_marginRight="16dp"

/>

android:id="@+id/gps_button_history"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/gps_history_button"

android:layout_marginBottom="8dp"

android:layout_marginLeft="16dp"

android:layout_marginTop="8dp"

android:layout_marginRight="16dp"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/gps_text"

android:paddingBottom="8dp"

android:paddingTop="8dp"

android:textSize="15sp"

android:layout_gravity="center"

android:gravity="center"

/>

android:id="@+id/bmapView"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:clickable="true" />

标注覆盖物实现代码如下:

/**

* 根据手表的经纬度在地图上设置位置,更新顶部的位置文字描述

*/

private void updateLocation(ArrayList list) {

/**

* 1. 新用户默认显示地址

*/

double lg = 104.06; // 成都市的经纬度

double lt = 30.67;

String location = getResources().getString(R.string.fake_position);

baiduMap.clear();

List potions = new ArrayList<>();

for(int i = list.size() -1; i >=0 ; i--){

// gps 非空,则设置经纬度、位置的文字描述

lg = Double.parseDouble(list.get(i).getLongitude());

lt = Double.parseDouble(list.get(i).getLatitude());

location = list.get(i).getAddress();

//地址太长,处理下

location = location.replace("四川省","").replace("成都市","").replace(".","");

final LatLng ll = new LatLng(lt, lg); // 注意经纬度顺序

/**

* 为每个足迹依据先后顺序编号

*/

int image_id = 0;

switch (i){

case 9: image_id = R.mipmap.icon_mark1;break;

case 8: image_id = R.mipmap.icon_mark2;break;

case 7: image_id = R.mipmap.icon_mark3;break;

case 6: image_id = R.mipmap.icon_mark4;break;

case 5: image_id = R.mipmap.icon_mark5;break;

case 4: image_id = R.mipmap.icon_mark6;break;

case 3: image_id = R.mipmap.icon_mark7;break;

case 2: image_id = R.mipmap.icon_mark8;break;

case 1: image_id = R.mipmap.icon_mark9;break;

case 0: image_id = R.mipmap.icon_mark10;break;

}

BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(image_id);

OverlayOptions options = new MarkerOptions().position(ll).icon(descriptor).zIndex(i);

Marker marker = (Marker) baiduMap.addOverlay(options);

//为marker添加识别标记信息

Bundle bundle = new Bundle();

bundle.putSerializable("info", list.get(i));

marker.setExtraInfo(bundle);

MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(ll,17);

baiduMap.setMapStatus(update);

}

为标记的marker添加监听,点击时实现弹出infowindow。(infowindow每次最多显示一条信息)

/**

* 为标记添加监听

* 点击弹出地理位置信息

*/

baiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {

@Override

public boolean onMarkerClick(Marker marker) {

// 获得marker中的数据

GPSBean info = (GPSBean) marker.getExtraInfo().get("info");

// 生成一个TextView用户在地图中显示InfoWindow

TextView location = new TextView(getApplicationContext());

location.setBackgroundResource(R.mipmap.gps_button);

location.setPadding(15, 15, 8, 35);

location.setTextColor(Color.DKGRAY);

location.setText("定位时间:" + info.getUploadTime() + "\n" + info.getAddress());

location.setTextSize(12);

// 将marker所在的经纬度的信息转化成屏幕上的坐标

final LatLng ll = marker.getPosition();

Point p = baiduMap.getProjection().toScreenLocation(ll);

p.y -= 47;

LatLng llInfo = baiduMap.getProjection().fromScreenLocation(p);

// 为弹出的InfoWindow添加点击事件

mInfoWindow = new InfoWindow(location,llInfo, new InfoWindow.OnInfoWindowClickListener() {

@Override

public void onInfoWindowClick() {

baiduMap.hideInfoWindow();

}

});

// 显示最后一条的InfoWindow

baiduMap.showInfoWindow(mInfoWindow);

return true;

}

});

最后将地图显示到最新的数据,并且显示Infowindow。

/**

* 显示最后一个地理位置信息

* 创建InfoWindow展示的view

*/

private void updateBaidumapInfowindown(String location,double lt, double lg){

TextView textView = new TextView(getApplicationContext());

textView.setBackgroundResource(R.mipmap.gps_button);

textView.setPadding(15, 15, 8, 35);

textView.setTextColor(Color.DKGRAY);

textView.setText(location);

textView.setTextSize(12);

final LatLng ll = new LatLng(lt,lg);

mInfoWindow = new InfoWindow(textView, ll, new InfoWindow.OnInfoWindowClickListener() {

@Override

public void onInfoWindowClick() {

baiduMap.hideInfoWindow();

}

});

//显示InfoWindow

baiduMap.showInfoWindow(mInfoWindow);

/**

* 将地图视野移动最新的位置

*/

MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(ll,17);

baiduMap.setMapStatus(update);

}

至此可以完成。但是还有最重要的一点,则是百度Api版本的问题,本方法在新版本 3.4.1 似乎就不能用这种方法实现,只能使用3.0.0版本。

android 百度map 一个layout加载多个mapview,android 百度地图API 使用Marker和InfoWindow相关推荐

  1. 【Android 安全】DEX 加密 ( 不同 Android 版本的 DEX 加载 | Android 8.0 版本 DEX 加载分析 | Android 5.0 版本 DEX 加载分析 )

    文章目录 一.不同版本的 DEX 加载 1.Android 8.0 版本 DEX 加载分析 2.Android 6.0 版本 DEX 加载分析 3.Android 5.0 版本 DEX 加载分析 一. ...

  2. Android设计一个图片加载框架

    本文不是具体编码去实现一个图片加载的框架,而是从理论上来讲解设计一个图片加载框架的注意事项和涉及的知识点,提供一个思路,或者帮助童鞋们应付面试.目前Android 发展至今优秀的图片加载框架太多,例如 ...

  3. Android中使用x5内核加载网页的实现

    前言 联系方式 背景 SDK下载 SDK集成 使用 代码实现 前言 由于是使用的腾讯浏览服务,所以这里大部分介绍的是官网的一些东西,不过自己会做一些复杂使用部分的实现,不至于像官网上介绍的笼统. 联系 ...

  4. Android Arcgis入门(12)、加载天地图

    在项目中可以经常需要动态加载一些图层,像投影地图服务.投影地图服务器.其实网上有大量这样的服务,比如天地图官网, . 随便点开一个服务,里面有相关的信息.那如何加载这样图层服务呢. 一.首先感谢这篇博 ...

  5. 【转载】一行代码加载网络图片到ImageView——Android Picasso

    原文链接:一句代码加载网络图片到ImageView--Android Picasso  注意:此处使用下面代码需要先配置一下gradle,下载所需包. 具体操作如下图: compile 'com.sq ...

  6. android多种方式实现异步加载图片

    记得之前做安卓应用时都是在2.2以下的版本,如果在UI线程中进行耗时操作,比如http,socket等 会产生android.os.NetworkOnMainThreadException 如果异步加 ...

  7. android实现新闻内容显示功能,Android开发实现自定义新闻加载页面功能实例

    本文实例讲述了Android开发实现自定义新闻加载页面功能.分享给大家供大家参考,具体如下: 一.概述: 1.效果演示: 2.说明:在新闻页面刚加载的时候,一般会出现五种状态 未知状态(STATE_U ...

  8. Android动画之仿美团加载数据等待时,小人奔跑进度动画对话框(附顺丰快递员奔跑效果)...

    Android动画之仿美团加载数据等待时,小人奔跑进度动画对话框(附顺丰快递员奔跑效果) 首句依然是那句老话,你懂得! finddreams :(http://blog.csdn.net/finddr ...

  9. Android实现异步从网络加载图片列表

     博文出处:http://blog.csdn.net/carterjin/article/details/7995935 有时会有在加载ListView的时候,包含用户头像或其他需要到网络获取的图 ...

最新文章

  1. jffs2 告警 和 一般性错误
  2. opencv中xml/yml文件操作类
  3. Servlet(1)
  4. Java 0xffffffff隐式类型转换的坑
  5. 堆之二项堆(Binominal Heap)
  6. bee 字符串转int_beego中gbk和utf8编码转换问题
  7. malloc()与calloc区别
  8. 对象列表Python概述:C++程序员眼中的Python
  9. 安装fastDFS的依赖包fdfs_client报错解决方法
  10. python小脚本获取抖音直播源的demo
  11. 品优影视源码 带会员中心+卡密系统
  12. 计算机wmi配置错误,系统没有WMI服务怎么办、WMI错误修复方法
  13. c语言例题18:完全平方数
  14. 离散数学-----自然数系统
  15. Django应用及分布式路由
  16. ffmpeg js转换音频_微信FFMPEG 扩展转换音频格式
  17. 高精度计算(三)压位
  18. matlab仿真高尔顿正态分布源码,中心极限定理:从高尔顿板到麦克斯韦分布
  19. 照明开关雷达感应方案,多普勒雷达模块技术,智能雷达感控应用
  20. torch.chunk用法

热门文章

  1. 单层神经网络线性回归_单层神经网络| 使用Python的线性代数
  2. 电源变换适用于非独立源码_适用于非None测试的Python程序
  3. Redis 持久化——混合持久化
  4. Debian11镜像更新为阿里巴巴开源镜像站镜像,切换root用户,解决用户名不在sudoers文件中此事将被报告,Debian11 文件夹对话框、火狐浏览器、命令终端等没有最大化和最小化
  5. [深入学习C#]LINQ查询表达式详解(1)——基本语法、使用扩展方法和Lambda表达式简化LINQ查询
  6. IDEA访问数据库时,某一个字段数据库中有值但是访问到的数据始终是null
  7. linux7 dns正向,Centos 7 搭建DNS正向解析和反向解析
  8. c语言如何把变量按位颠倒,求答案,用C语言编程,用户输入一个正整数,把他的各位数字前后颠倒,并输入点到后的结果...
  9. java run里面定义变量_Java程序员50多道最热门的多线程和并发面试题(答案解析)...
  10. edge浏览器怎么恢复默认设置 edge恢复默认设置方法