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

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

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

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

0818b9ca8b590ca3270a3433284dd417.png

布局文件很简单,主要就是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版本。

0818b9ca8b590ca3270a3433284dd417.png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/558468.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python数据的格式输出_Python格式化输出

“%”的使用 格式符 描述 %s 字符串 (采用str()的显示) %r 字符串 (采用repr()的显示) %c 单个字符及其ASCII码 %u 整数(无符号) %b 二进制整数 %o 八进制数(无符号) %d 十进制整数 %i 十进制整数 %x 十六进制数(无符号) %X 十六进制数大写(无符号) %e 指数 (基底写为e)&#x…

android代码移除焦点,android-如何从单个editText移除焦点

android-如何从单个editText移除焦点在我的应用程序中&#xff0c;我只有一个button.seFocusableInTouchMode()&#xff0c;以及一些button.requestFocus()&#xff0c;按钮和一个微调器。 我认为&#xff0c;我的EditText获得关注&#xff0c;因为它是此活动中唯一可关注的视图…

python数据分析实验报告_Python 数据分析入门实战

本训练营中&#xff0c;我们将学习怎么样使用 Python 进行数据分析。课程将从数据分析基础开始&#xff0c;一步步深入讲解。从 Python 的基础用法到数据分析的各种算法&#xff0c;并结合各种实例&#xff0c;讲解数据分析过程中的方方面面。 课程内容将分为以下四个部分&…

unistd.h linux,Linux 标准库下的unistd.h

unistd.h在unix中类似于Window中的windows.h!#ifdef WIN32#include#else#include#endifunistd.h含有的常量与函数&#xff1a;ssize_t read(int, void *,size_t);ssize_t write(int, const void *,size_t);int unlink(const char *);int us…

go 写文件_如何在 Ubuntu 20.04 上安装 Go

本文最先发布在&#xff1a;如何在 Ubuntu 20.04 上安装 Go​www.itcoder.techGo&#xff0c;通常被称为 golang&#xff0c;它是一门由 Google 创建的现代化的开源编程语言&#xff0c;它允许你构建实时并且高效的应用。很多流行的应用程序&#xff0c;例如 Kubernetes&#x…

android纹理存储,Android:OpenGL存储纹理多长时间?

openGL存储纹理多长时间&#xff1f;离开活动时纹理内存是否会被回收&#xff1f;例如,如果我有以下代码:mGL.glGenTextures(1, mTextures, 0);mGL.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]); // A bound texture is// an active texture//mGL.glTexImage2D(GL10.GL_TE…

python获取文件夹名_python基础之获取文件目录及名称

准备 被引用的文件&#xff1a;D:\gogncheng\apiAutoMate\api\a\bePerform.py 执行的文件&#xff1a; D:\gogncheng\apiAutoMate\common\b\perform.py 适用场景&#xff1a;在perform.py下引用文件bePerform.py运行代码&#xff0c;分别获取引用文件与执行文件的目录及名称 be…

android调用fragment的方法,AndroidX下使用Activity和Fragment的变化

原标题&#xff1a;AndroidX下使用Activity和Fragment的变化原文&#xff1a;How AndroidX changes the way we work with Activities and Fragments作者&#xff1a;Miłosz Lewandowski译者&#xff1a;Fly_with24链接&#xff1a;https://juejin.im/post/5e5a0c316fb9a07cd2…

java汽车管理系统_坑爹!花费2亿耗时2年,网站没建完Java都写不好,顶级咨询公司埃森哲被告上法庭...

乾明 发自 凹非寺 量子位 报道 | 公众号 QbitA耗费2个多亿&#xff0c;耗时2年多&#xff0c;连一个可用的网站或者APP都没有交付出来。想要完工&#xff1f;那就再交1000万美元。这件事的受害方、美国汽车租赁公司赫兹(Hertz)一怒之下&#xff0c; 将顶级咨询公司埃森哲(Accen…

Android接口一般定义格式,Android开发规范

原标题&#xff1a;Android开发规范一.书写规范1. 编码方式统一用UTF-8.2. 花括号不要单独一行&#xff0c;和它前面的代码同一行。而且&#xff0c;花括号与前面的代码之间用一个空格隔开。3. 空格的使用if、else、for、switch、while等逻辑关键字与后面的语句留一个空格隔开。…

c++将小写转换为大写函数_必须掌握的基础函数组合应用技巧,提高效率,准时下班...

点击上方"Excel函数公式"免费订阅货币&#xff0c;生活中必不可少的东西&#xff0c;是物品价值等的直接体现&#xff0c;在实际的工作中也经常遇到&#xff0c;如果给定的数据中&#xff0c;要对其进行格式的设置&#xff0c;你会怎么做&#xff1f;一、Dollar函数&…

android 关闭软键盘失去焦点,Android - html输入在软键盘打开时失去焦点(ASP.net)...

使用Nexus 4&#xff0c;Android 4.2.1本机Chrome浏览器 - 当我点击字段时&#xff0c;软键盘出现&#xff0c;然后该字段立即失去焦点。我必须再次点击字段&#xff0c;并且已经打开键盘才能输入文字。这不会发生在桌面上的Chrome中。我在ASP用户控制下的登录表单&#xff1a;…

jenkins使用哪个版本号_Linux下安装JDK及jenkins

往期相关文章推荐&#xff1a;Linux ping不通域名安装JDK依赖(8/11)一.(推荐)// 查看yum仓库中可安装的jdk版本yum -y list java*// 安装示例yum install -y java-1.8.0-openjdk-devel.x86_64java --version 二.1.获取JDK安装包&#xff0c;可以win下下载&#xff0c;再用scp…

骁龙660鸿蒙系统,骁龙660双摄测试机偷跑 核心数/GPU证实

中关村在线消息&#xff1a;高通会在今年推出一款全新的中端处理器——骁龙660。此前有网友在微博上曝光一组疑似骁龙660的跑分&#xff0c;安兔兔总成绩为105576分。现在&#xff0c;微博上又出现搭载骁龙660双摄工程机的谍照&#xff0c;该机支持2K分辨率&#xff0c;采用6GB…

tensorflow 模型可视化_基于tensorflow-2.x的yolov3实现

YOLO v3可以说是单阶段检测器中的佼佼者&#xff0c;融合了多个框架的优势&#xff0c;在保持模型简洁性的同时&#xff0c;性能上也在当时达到了stoa。YOLO v3的主干网络是darknet-53的前面的52层&#xff0c;所以它是一个全卷积网络&#xff0c;并且为了降低池化带来的梯度负…

android闹钟延时,android闹钟定时启动延时或者直接不启动

自己写的android闹钟功能&#xff0c;需要实现timepicker选择完成后将选择的时间设定为闹钟的启动时间&#xff0c;但是不管怎么改总是没法定时启动alertDialog new AlertDialog.Builder(context).setView(view).setCustomTitle(viewTitle).setNegativeButton("确定"…

switch语句可以被代替吗_爬楼梯可以代替跑步吗?

转载&#xff1a;有很多人在下雨天选择爬楼梯作为运动方式&#xff0c;前几天就有人问老王&#xff1a;爬楼梯可以代替跑步吗&#xff1f;爬楼梯是在一个坡度上下移动&#xff0c;上楼梯时&#xff0c;腿部需要承受自身体重1.5-2.5倍的重量&#xff1b;下楼梯时则要承受自身体重…

android 命令使用详解,Android下pm 命令详解

作者&#xff1a;Sam (甄峰) sam_codehotmail.comSam在看相关PackageManager代码时&#xff0c;无意中发现Android下提供一个pm命令&#xff0c;通常放在/system/bin/下。这个命令与Package有关&#xff0c;且非常实用。所以研究之。0. Usage&#xff1a;usage: pm [list|path|…

html页面加载时执行ajax请求,页面加载完成之后,ajax远程调用的数据才显示出来。有没有可能使ajax部分的程序执行完了,页面再加载完成...

王不懒(作者)回复 DCloud_UNI_FXY:mui.plusReady(function(){var now plus.webview.currentWebview();now.hide()//console.log(now.hide());//now.hide();mui.post("xxxx.html",{},function(data){var goods document.getElementById(goods);for (var i0; ivar op…

gsonformat插件_吐血推荐珍藏的IDEA插件

之前给大家推荐了一些我自己常用的VS Code插件&#xff0c;很多同学表示很受用&#xff0c;并私信我说要再推荐一些IDEA插件。作为一名职业Java程序员/业余js开发者&#xff0c;我平时还是用IDEA比较多&#xff0c;所以也确实珍藏了一些IDEA插件。今天就一并分享给大家。在最开…