Android 百度地图定位(手动+自动) 安卓开发教程
近由于项目需要,研究了下百度地图定位,他们提供的实例基本都是用监听器实现自动定位的。我想实现一种效果:当用户进入UI时,不定位,用户需要定位的时候,自己手动点击按钮,再去定位当前位置。 经过2天研究和咨询,找到了解决方案,在此备忘一下。
注意:定位使用真机才能够真正定位;模拟器的话,在DDMS中的Emulator Control中,选择Manual,下面单选按钮选择Decimal,然后填写经纬度,send后,再点击定位我的位置按钮,就能定位了(这应该算是固定定位,哈哈。。。)、
1、第一步当然是获取一个针对自己项目的key值。http://dev.baidu.com/wiki/static/imap/key/
2、使用百度API是有前提的,摘自百度:首先将API包括的两个文件baidumapapi.jar和 libBMapApiEngine.so拷贝到工程根目录及libs\armeabi目录下,并在工程属性->Java Build Path->Libraries中选择“Add JARs”,选定baidumapapi.jar,确定后返回,这样您就可以在您的程序中使用API了。(这两个文件见附件)。
3、按照自己的需求写一个layout,我的如下:
<?xml version="1.0" encoding="utf-8"?>
Xml代码
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myLocation_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center_horizontal"
android:textColor="@drawable/black"
android:background="@drawable/gary"
/>
<com.baidu.mapapi.MapView android:id="@+id/bmapsView"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:clickable="true" android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/location_button_id"
android:text="@string/location_button_text"
/>
</LinearLayout>
需要特别注意的是:<com.baidu.mapapi.MapView /> 这玩意。
4、写一个MapApplication实现application,提供全局的BMapManager,以及其初始化。
Java代码
public BMapManager mapManager = null;
static MapApplication app;
public String mStrKey = "你申请的key值";
@Override
public void onCreate() {
mapManager = new BMapManager(this);
mapManager.init(mStrKey, new MyGeneralListener());
}
@Override
//建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗
public void onTerminate() {
// TODO Auto-generated method stub
if(mapManager != null)
{
mapManager.destroy();
mapManager = null;
}
super.onTerminate();
}
static class MyGeneralListener implements MKGeneralListener{
@Override
public void onGetNetworkState(int arg0) {
Toast.makeText(MapApplication.app.getApplicationContext(), "您的网络出错啦!",
Toast.LENGTH_LONG).show();
}
@Override
public void onGetPermissionState(int iError) {
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
// 授权Key错误:
Toast.makeText(MapApplication.app.getApplicationContext(),"您的授权Key不正确!",
Toast.LENGTH_LONG).show();
}
}
}
5、接下来就是按照百度api写定位代码了,使用handler机制去添加定位图层,需要说明的都在注释上了。
private BMapManager mBMapMan = null;
private MapView mMapView = null;
private MapController bMapController;
private MKLocationManager mkLocationManager;
private MKSearch mkSearch;
private TextView address_view; //定位到的位置信息
private ProgressDialog dialog;
private List<HotelInfo> hotelList;
private int distance = 1000; //查询的范围(单位:m)
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
double lat = msg.getData().getDouble("lat");
double lon = msg.getData().getDouble("lon");
if(lat!=0&&lon!=0){
GeoPoint point = new GeoPoint(
(int) (lat * 1E6),
(int) (lon * 1E6));
bMapController.animateTo(point); //设置地图中心点
bMapController.setZoom(15);
mkSearch.reverseGeocode(point); //解析地址(异步方法)
MyLocationOverlay myLoc = new MyLocationOverlayFromMap(ShowMapAct.this,mMapView);
myLoc.enableMyLocation(); // 启用定位
myLoc.enableCompass(); // 启用指南针
mMapView.getOverlays().add(myLoc);
}else{
Toast.makeText(ShowMapAct.this, "没有加载到您的位置", Toast.LENGTH_LONG).show();
}
if(hotelList!=null){
Drawable marker = getResources().getDrawable(R.drawable.iconmarka); //设置marker
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight()); //为maker定义位置和边界
mMapView.getOverlays().add(new OverItemList(marker,hotelList,ShowMapAct.this,bMapController));
}else if(hotelList==null&&lat!=0&&lon!=0){
Toast.makeText(ShowMapAct.this, "网络异常,没有获取到酒店信息。", Toast.LENGTH_LONG).show();
}
if(dialog!=null) dialog.dismiss();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
distance = getIntent().getExtras().getInt("distance"); //获取查询范围
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
mMapView = (MapView)findViewById(R.id.bmapsView); //初始化一个mapView 存放Map
init(); //初始化地图管理器
super.initMapActivity(mBMapMan);
address_view = (TextView)findViewById(R.id.myLocation_id);
SpannableStringBuilder style = new SpannableStringBuilder(String.format(getResources().getString(R.string.location_text),"位置不详"));
style.setSpan(new ForegroundColorSpan(Color.RED), 5, style.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
address_view.setText(style);
Button location_button = (Button)findViewById(R.id.location_button_id);
location_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(ShowMapAct.this, "", "数据加载中,请稍后.....");
new Thread(new MyThread()).start();
}
});
mkSearch = new MKSearch(); //初始化一个MKSearch,根据location解析详细地址
mkSearch.init(mBMapMan, this);
mMapView.setBuiltInZoomControls(true); //启用内置的缩放控件
bMapController = mMapView.getController();
GeoPoint defaultPoint = new GeoPoint((int) (39.920934 * 1E6),(int) (116.412817 * 1E6)); //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)
bMapController.setCenter(defaultPoint); //设置地图中心点
bMapController.setZoom(12); //设置地图zoom级别
mkLocationManager = mBMapMan.getLocationManager();
}
/**
* 初始化地图管理器BMapManager
*/
public void init(){
MapApplication app = (MapApplication)getApplication();
if (app.mapManager == null) {
app.mapManager = new BMapManager(getApplication());
app.mapManager.init(app.mStrKey, new MapApplication.MyGeneralListener());
}
mBMapMan = app.mapManager;
}
@Override
protected void onDestroy() {
MapApplication app = (MapApplication)getApplication();
if (mBMapMan != null) {
mBMapMan.destroy();
app.mapManager.destroy();
app.mapManager = null;
mBMapMan = null;
}
super.onDestroy();
}
@Override
protected void onPause() {
if (mBMapMan != null) {
// 终止百度地图API
mBMapMan.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if (mBMapMan != null) {
// 开启百度地图API
mBMapMan.start();
}
super.onResume();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onGetAddrResult(MKAddrInfo result, int iError) {
if(result==null) return;
SpannableStringBuilder style = new SpannableStringBuilder(String.format(getResources().getString(R.string.location_text),result.strAddr));
style.setSpan(new ForegroundColorSpan(Color.RED), 5, style.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
address_view.setText(style);
if(dialog!=null) dialog.dismiss();
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {}
/**
* 重新定位,加载数据
* @author Administrator
*
*/
class MyThread implements Runnable{
@Override
public void run() {
/**
* 最重要的就是这个玩意
* 由于LocationListener获取第一个位置修正的时间会很长,为了避免用户等待,
* 在LocationListener获取第一个更精确的位置之前,应当使用getLocationInfo() 获取一个缓存的位置
*/
Location location = mkLocationManager.getLocationInfo();
double lat = 0d,lon = 0d;
if(location!=null){ //定位到位置
String coordinate = location.getLatitude()+","+location.getLongitude();
HotelRemoteData hotelData = new HotelRemoteData();
/**
* 远程获取酒店列表数据
*/
hotelList = hotelData.getHotelToMap(coordinate,distance);
lat = location.getLatitude();
lon = location.getLongitude();
}
Message msg = new Message();
Bundle data = new Bundle();
data.putDouble("lat", lat);
data.putDouble("lon", lon);
msg.setData(data);
handler.sendMessage(msg);
}
}
6、还有一种就是百度示例相当推荐的,也是加载定位位置速度比较快的,那就是通过定位监听器来定位信息。没啥难的,照着百度的示例写,都能搞定。
Java代码
LocationListener listener = new LocationListener() {
@Override
/** 位置变化,百度地图即会调用该方法去获取位置信息。
* (我测试发现就算手机不动,它也会偶尔重新去加载位置;只要你通过重力感应,他就一定会重新加载)
*/
public void onLocationChanged(Location location) {
GeoPoint gp = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6)); //通过地图上的经纬度转换为地图上的坐标点
bMapController.animateTo(gp); //动画般的移动到定位的位置
}
};
Android 百度地图定位(手动+自动) 安卓开发教程的更多相关文章
- android 百度地图定位功能实现
历经几天时间,终于把定位功能给实现了,可谓是费劲千辛万苦啊,有定位知识还有图层知识,在这里我把代码给大家贴出来,一起分享一下下啦. package com.example.foreveross.off ...
- 我的Android进阶之旅------>Android百度地图定位SDK功能学习
因为项目需求,需要使用百度地图的定位功能,因此去百度地图开发平台下载了百度地图的Android定位SDK最新版本的开发包和示例代码学习. Android 定位SDK地址:http://develope ...
- Android百度地图定位
在谈到百度地图.如今,每个人都知道这个时候应该可以了吧.而更多的字不拉.直接朝话题. 访问百度地图api您必须应用key,应用在这里key不用说,有官方的文件说明如何应用上述key. 在这里,百度地图 ...
- android 百度地图定位开发2
先下载了示例代码 进行配置(可查看开发 指南 Hello BaiDuMap) 第一步:创建并配置工程(具体方法参见工程配置部分的介绍): 第二步:在AndroidManifest中添加开发密钥.所需权 ...
- android 百度地图定位开发1
首先注册成为百度开发者 然后进入百度开发者中心 点击LBS 跳到下一个页面 点击Android 开发 里面的基础地图 进入 点击获取密钥 进入 点击创建应用 进入 应用名称自己填 应用类 ...
- android百度地图定位开发
一.activity import android.app.Activity; import android.graphics.Point;import android.graphics.PointF ...
- android 百度地图 定位功能
废话不多说 直接新建一个新android项目:location,然后花一分钟申请一个key,然后就是把百度定位demo抄一下即可 1:首先在AndroidManifest.xml中加入权限 <u ...
- android 百度地图 定位获取位置失败 62错误
一切正常步骤进行但是还是没有获得定位,得到的坐标总是49E.xxxx,错误代码总是62 总是以为代码.或jar包.或还有什么权限没给.搞了好久,十分郁闷.在控制台上又没有什么具体的错误提示 经过无数次 ...
- 050 Android 百度地图的使用
1.初始化地图 //初始化地图 private void initMapView() { //1.获取地图控件引用 mMapView = findViewById(R.id.bmapView); mB ...
随机推荐
- C使用FILE指针文件操作
文件的基本概念 所谓“文件”是指一组相关数据的有序集合. 这个数据集有一个名称,叫做文件名.实际上在前面的各章中我们已经多次使用了文件,例如源程序文件.目标文件.可执行文件.库文件 (头文件)等.文件 ...
- 设计模式知识搜集(c++)
理解设计模式有两种途径,一种是看UML类图,一种是看代码理解实例,UML(一个简单的介绍)看懂了对理解设计模式大有裨益,代码容易陷进去,因此最好能找到适当且易于理解的应用场景,这下面实际上每个都是我收 ...
- java、android 对比两个目录或文件是否是同一个目录或文件的方法
由于软链接及android的外部卡mount方式存在,导致一个文件夹可能同时有两个路径,如: /mnt/sdcard1 /storage/ext_sdcard ,如果通过某种方式(如moun ...
- ef6 code first
http://www.cnblogs.com/Bce-/p/3684643.html http://www.cnblogs.com/Gyoung/tag/Entity%20Framework/ htt ...
- 关于MAC下的QQ聊天中看不到对方所发的图片解决
使用QQ聊天我们会经常碰到一件让人烦心的事情,那就是别人发的截图自己看不大,是一张裂图(腾讯默认的那张图片).通常有几种情况可以造成这种结果: 第一种原因,网络延迟原因,你的网络不好或者对方的网络不好 ...
- cocos2d-x3.2下获取文件夹下所有文件名的方法
这里提供一个函数获取文件夹下所有文件名的方法,直接上代码了. 原文地址:http://blog.csdn.net/qqmcy/article/details/36184733 // // Visib ...
- CSS 居中大全
<center> text-align:center 在父容器里水平居中 inline 文字,或 inline 元素 vertical-align:middle 垂直居中 inline 文 ...
- SA
hdu 4029 题意:给你一个字符矩阵,统计不同的子矩阵的个数: 分析:枚举子矩阵的宽度w,对于每一个w,将每一行长度可以是w的字符串HASH成一个值,然后用map标记,因为宽确定了,hash完之后 ...
- oracle关键字使用
v_Describe:=substr(v_Describe,0,length(v_Describe)-1); substr(目标内容,开始位置,截取长度) length(要计算的内容长度) 上述语句可 ...
- java 对excel操作导入excel数据到数据库
加入jar包jxl.jar ===================services层掉用工具类==================================== // 导入 public Lis ...