android百度地图更换定位图标,android百度地图定位,改变MyLocationOverlay默认图标(原始为蓝色点)(两种方法)...
开放地图API无外乎google ,百度,高德等.其它的还有很多,不过比较好用的就这三种了,如果不需要出国(台湾例外),则最好使用百度地图,性能比高德好的多,无聊的时候自己可以测试测试.。
下面我简单介绍下百度地图的应用.首先地图所需要的key就不多说了.。
在这里我要说明一点:大家在加入jar包的时候一定要加入到libs文件夹中,(不可以是lib),不然运行会出现下面这个错误,我也为此纠结过.希望对你们有帮助,在此write it for you.
大家可以参考官方文档和给出的DEMO进行学习,有问题大家一起研究.
在这里我重点说明一下如何改变百度地图在定位自己时的默认蓝色圆圈.
Overlay:覆盖物的抽象基类,所有的覆盖物均继承此类的方法,实现用户自定义图层显示。
MyLocationOverlay:一个负责显示用户当前位置的Overlay。
第一种方法很简单,我们只需要创建一个继承MyLocationOverlay的类重写其drawMyLocation方法即可.
protectedvoiddrawMyLocation(android.graphics.Canvas canvas,
MapView mapView,
android.location.Location lastFix,
GeoPoint myLocation,
longwhen)
protected void drawMyLocation(android.graphics.Canvas canvas,
MapView mapView,
android.location.Location lastFix,
GeoPoint myLocation,
long when)
绘制“我的位置”点。默认情况下,绘制一个动画的“蓝色点”,可能由一个蓝色圆盘所围绕来标识精度。而且, 如果用户的位置移动到屏幕的边缘,我们已经在构造函数力提供了一个 MapController , 将使用滚动来重新确定地图的中心。
Parameters:
canvas - 待绘制的画布。
mapView - 调用绘制方法的mapView。
lastFix - 最后一个收到的位置信息。
myLocation - 最后一个位置的坐标,萃取成为一个方便的GeoPoint 。
when - 绘制的时间,毫秒。
protectedvoiddrawMyLocation(Canvas canvas, MapView mapView,
Location lastFix, GeoPoint myLocation, longwhen) {
try{
Projection projection = mapView.getProjection();
Point point = newPoint();
projection.toPixels(myLocation, point);
intx = point.x - bitmap.getWidth() / 2;
inty = point.y - bitmap.getHeight();
canvas.drawBitmap(bitmap, x, y, newPaint());
} catch(Exception e) {
super.drawMyLocation(canvas, mapView, lastFix, myLocation, when);
}
}
protected void drawMyLocation(Canvas canvas, MapView mapView,
Location lastFix, GeoPoint myLocation, long when) {
try {
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(myLocation, point);
int x = point.x - bitmap.getWidth() / 2;
int y = point.y - bitmap.getHeight();
canvas.drawBitmap(bitmap, x, y, new Paint());
} catch (Exception e) {
super.drawMyLocation(canvas, mapView, lastFix, myLocation, when);
}
}
这里简单说明一下:point获取到的是我们在屏幕上的点.而我们要显示的图标应该在这个点的正上方,因此x,y的计算大家都不难理解了.
接下来的操作和原来的一样,只需要调用即可.
voidInitMap() {
bMapManager = newBMapManager(this);
bMapManager.init(Key, this);
super.initMapActivity(bMapManager);
mMapView = (MapView) findViewById(R.id.bmapsView);
mMapView.setBuiltInZoomControls(true);// 设置启用内置的缩放控件
mMapView.setDrawOverlayWhenZooming(true);// 缩放图标仍然show
mMapController = mMapView.getController();
mMapController.setZoom(16); // 设置地图zoom级别
}
void InitMap() {
bMapManager = new BMapManager(this);
bMapManager.init(Key, this);
super.initMapActivity(bMapManager);
mMapView = (MapView) findViewById(R.id.bmapsView);
mMapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件
mMapView.setDrawOverlayWhenZooming(true);// 缩放图标仍然show
mMapController = mMapView.getController();
mMapController.setZoom(16); // 设置地图zoom级别
}
voidaddMyOverLay() {
locationOverlay = newLocationOverLay(MainActivity.this, mMapView);
overlays = mMapView.getOverlays();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.location);
locationOverlay.setBitmap(bitmap);
overlays.add(locationOverlay);
}
void addMyOverLay() {
locationOverlay = new LocationOverLay(MainActivity.this, mMapView);
overlays = mMapView.getOverlays();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.location);
locationOverlay.setBitmap(bitmap);
overlays.add(locationOverlay);
}
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("jjhappyforever...");
InitMap();
addMyOverLay();
}
@Override
protectedvoidonDestroy() {
if(bMapManager != null) {
bMapManager.destroy();
bMapManager = null;
}
super.onDestroy();
}
@Override
protectedvoidonPause() {
if(bMapManager != null) {
bMapManager.getLocationManager().removeUpdates(this);
locationOverlay.disableMyLocation();
locationOverlay.disableCompass(); // 打开指南针
bMapManager.stop();
}
super.onPause();
}
@Override
protectedvoidonResume() {
if(bMapManager != null) {
bMapManager.getLocationManager().requestLocationUpdates(this);
locationOverlay.enableMyLocation();
locationOverlay.enableCompass(); // 打开指南针
bMapManager.start();
}
super.onResume();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("jjhappyforever...");
InitMap();
addMyOverLay();
}
@Override
protected void onDestroy() {
if (bMapManager != null) {
bMapManager.destroy();
bMapManager = null;
}
super.onDestroy();
}
@Override
protected void onPause() {
if (bMapManager != null) {
bMapManager.getLocationManager().removeUpdates(this);
locationOverlay.disableMyLocation();
locationOverlay.disableCompass(); // 打开指南针
bMapManager.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if (bMapManager != null) {
bMapManager.getLocationManager().requestLocationUpdates(this);
locationOverlay.enableMyLocation();
locationOverlay.enableCompass(); // 打开指南针
bMapManager.start();
}
super.onResume();
}
/***
* Location 监听
*
* @param arg0
*/
@Override
publicvoidonLocationChanged(Location location) {
// 获取自己的经纬度点
GeoPoint geoPoint = newGeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
mMapController.setCenter(geoPoint);
}
/***
* Location 监听
*
* @param arg0
*/
@Override
public void onLocationChanged(Location location) {
// 获取自己的经纬度点
GeoPoint geoPoint = new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
mMapController.setCenter(geoPoint);
}
定位居中显示.
示例显示:
第二种方案:
我们bMapManager.getLocationManager().requestLocationUpdates(this);获取自身的GeoPoint.那么我们可以根据该GeoPoint创建一个Overlay.其实MyLocationOverlay是Overlay的一个子类,这样我们就不难理解了。发话不多说了,
在这里我们根据自身的GeoPoint添加一个Overlay
/***
* Location 监听
*
* @param arg0
*/
@Override
publicvoidonLocationChanged(Location location) {
// 获取自己的经纬度点
GeoPoint geoPoint = newGeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
mMapController.setCenter(geoPoint);
addOverLay(geoPoint);
}
[java]
/***
* 添加overlay
*/
voidaddOverLay(GeoPoint geoPoint) {
overlays = mMapView.getOverlays();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.location);
overlay = newMyOverlay(mMapView, bitmap);
overlay.setGeoPoint(geoPoint);
overlays.add(overlay);//添加自定义overlay
}
[java]
publicclassMyOverlay extends Overlay {
privateMapView mapView;
privateProjection projection;
privatePoint point;// 屏幕对象的点
privateBitmap bitmap;
privateGeoPoint geoPoint;// 经纬度点
publicvoidsetGeoPoint(GeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
publicMyOverlay(MapView mapView, Bitmap bitmap) {
super();
this.mapView = mapView;
this.bitmap = bitmap;
}
@Override
publicvoiddraw(Canvas canvas, MapView arg1, boolean arg2) {
projection = mapView.getProjection();
point = newPoint();
projection.toPixels(geoPoint, point);// 将GeoPoint 转换成point.
intx = point.x - bitmap.getWidth() / 2;
inty = point.y - bitmap.getHeight();
canvas.drawBitmap(bitmap, x, y, newPaint());
}
}
/***
* Location 监听
*
* @param arg0
*/
@Override
public void onLocationChanged(Location location) {
// 获取自己的经纬度点
GeoPoint geoPoint = new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
mMapController.setCenter(geoPoint);
addOverLay(geoPoint);
}
[java]
/***
* 添加overlay
*/
void addOverLay(GeoPoint geoPoint) {
overlays = mMapView.getOverlays();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.location);
overlay = new MyOverlay(mMapView, bitmap);
overlay.setGeoPoint(geoPoint);
overlays.add(overlay);//添加自定义overlay
}
[java]
public class MyOverlay extends Overlay {
private MapView mapView;
private Projection projection;
private Point point;// 屏幕对象的点
private Bitmap bitmap;
private GeoPoint geoPoint;// 经纬度点
public void setGeoPoint(GeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
public MyOverlay(MapView mapView, Bitmap bitmap) {
super();
this.mapView = mapView;
this.bitmap = bitmap;
}
@Override
public void draw(Canvas canvas, MapView arg1, boolean arg2) {
projection = mapView.getProjection();
point = new Point();
projection.toPixels(geoPoint, point);// 将GeoPoint 转换成point.
int x = point.x - bitmap.getWidth() / 2;
int y = point.y - bitmap.getHeight();
canvas.drawBitmap(bitmap, x, y, new Paint());
}
}
示例显示:
android百度地图更换定位图标,android百度地图定位,改变MyLocationOverlay默认图标(原始为蓝色点)(两种方法)...相关推荐
- android百度地图定位,改变MyLocationOverlay默认图标(原始为蓝色点)(两种方法)
开放地图API无外乎google ,百度,高德等.其它的还有很多,不过比较好用的就这三种了,如果不需要出国(台湾例外),则最好使用百度地图,性能比高德好的多,无聊的时候自己可以测试测试.. 下面我简单 ...
- RK3326 Android 8.1 修改默认输入法为讯飞输入法——两种方法,推荐第二种(纯代码)
方法一 1:预装对应的输入法,我使用的瑞芯微方案,已经提供了方法,把APK放在对应的目录里就会预装,不具备参考性 可以参考下面的博客: 是在Android7.0上操作的 https://blog.cs ...
- Android学习之为按钮添加事件监听器的两种方法
为按钮(包括普通按钮和图片按钮等)添加单击事件监听器有两种方法,第一种是利用匿名内部类来实现,第二种是使用onClick属性来实现 1.匿名内部类 使用匿名内部类,我们首先需要在布局文件中给按钮设置i ...
- brook客户端android,Android端线上NativeCrash收集的两种方法(一)
Android端crash可分为Java层crash和Native crash,我们通常说的crash一般指的是Java层crash,Native crash主要指C/C++代码(其在Android工 ...
- Android Studio导入Eclipse项目的两种方法
Android Studio导入Eclipse项目有两种方法,一种是直接把Eclipse项目导入Android Studio,另一种是在Eclipse项目里面进行转换,然后再导入Android Stu ...
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
这篇文章转自博客园 Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putPa ...
- 【错误记录】NDK 导入外部 so 动态库报错 ( java.lang.UnsatisfiedLinkError | Android Studio 配置外部 so 动态库两种方法 )
文章目录 一.报错信息 二.解决方案 ( Android Studio 配置外部 so 动态库两种方法 ) 1.jniLibs 目录存放 2.libs 目录存放 一.报错信息 外部引用 so 动态库 ...
- Android中添加背景音乐的两种方法
前些天在尝试自己写一个Android小游戏--flybird 基本功能实现了,就想添加声音,然后上网查了查,大多是一样,可是用到我这,有些却不可以用,所以我还用了两种方法. 下面谈谈这两种方法. 方法 ...
- Android中用GridView实现九宫格的两种方法(转)
Android中用GridView实现九宫格的两种方法 http://blog.csdn.net/shakespeare001/article/details/7768455 1.传统办法:实现一个继 ...
最新文章
- git push 不再需要重复输入账户密码的技巧
- mysql hibernate 延迟_Hibernate+Spring数据延迟加载问题解决方案
- android背景图拉伸,Android使背景图像不拉伸它指定的视图_android_开发99编程知识库...
- 打印某个user在指定时间段内做过的personalization detail
- 连载:告诉你如何设计一个日访问量千万级别的系统,谈oracle的高级设计和开发(2)...
- Win11如何设置滚动条 Win11滚动条设置教程
- 手机按三角返回页面上一页_小猿圈微信小程序跳转页面都有哪些?
- 企业class类命名规范
- 传感器市场需求大幅提升 中企能否满足?
- 计算机网络 简单网络管理协议 SNMP
- 用域代码任何带圈字符都能做出来
- window10计算机策略,Win10秘笈:重置组策略/安全策略命令大全
- 向 AppStore iOS 苹果appstore 提交新版本app出现问题
- 刺骨寒江合力托举老人上岸
- 干货分享!简单的python爬取网站数据。
- 【RGB手持补光棒调光照明方案】 单节双节电池LED升压恒流驱动调光芯片FP7208,PWM内部转模拟调光,无频闪顾虑低亮无抖动
- python3爬虫基本操作——抓取股票信息
- 多线程使用场景(经典必看)
- AI模型也需要资产管理,星环科技推出AI运营平台MLOps星环科技星环科技
- Fabric背书过程中链码是并行还是串行?
热门文章
- 我想说:mysql 的 join 真的很弱
- 3年国奖、一作9篇SCI,完美逆袭的中大博士坦言自己也曾濒临挂科
- gevent源码初探-wsgi例子解析
- MySQL数据库子查询
- ACMNO.27 Python的两行代码解决 C语言-字符逆序 写一函数。使输入的一个字符串按反序存放,在主函数中输入输出反序后的字符串。 输入 一行字符 输出 逆序后的字符串
- PyTorch深度学习训练可视化工具tensorboardX
- 基于OpenCV 的车牌识别
- 收藏 | 卷积神经网络中用1*1 卷积有什么作用或者好处呢?
- Nuxt.js - nuxt-link与router-link的差异
- Dubbo开源现状与未来规划