怎么用百度地图定位双方手机别人

您所在的位置:
  你是否知道手机百度地图中怎样进行定位?今天小编就讲解一下手机百度地图中进行定位的图文教程哦,下面我们就一起去学习学习吧。
  手机百度地图怎么定位?
  1、先打开手机的网络(数据流量或无线WiFi)和GPS定位
  2、打开手机百度地图,点击首页左下角的【指南针】标志
  3、定位成功,同时自动切换视图
  以上就是手机百度地图中进行定位的图文教程,希望可以帮助到大家。
微信被广泛应用,那么微信收款码贴纸如何申请,下面就是小编带来...
电脑软件排行
移动软件排行
读过此文的人还看过
天极大视野
京公网安备84号百度地图API首页
Android 定位SDKv4.0
百度地图Android定位SDK为基于移动客户端开发LBS应用提供基础定位能力,它较而言,专注满足用户获取当前位置与获得地址信息描述功能,软件包体积小。使用Android定位SDK,需先申请key,若你需要在同一个工程中同时使用定位SDK和地图SDK,可以共用同一个key。
百度Android定位SDK支持Android1.5以及以上设备,提供:
定位功能:通过GPS、网络定位(WIFI、基站)混合定位模式,返回当前所处的位置信息
反地理编码功能:解析当前所处的位置坐标,获得详细的地址描述信息。
地理围栏:通过定位SDK提供的定位服务和地理围栏服务,轻松实现基于位置提醒的各种服务
定位SDK支持多样化服务与定位策略,用户可通过设置不同的定位时间间隔、选择不同的定位服务模式、定制不同的定位结果信息来满足自身需求。
支持Android1.5及以上系统
使用百度Android定位SDK必须注册GPS和网络使用权限。定位SDK采用GPS、基站、Wi-Fi信号进行定位。当应用程序向定位SDK发起定位请求时,定位SDK会根据应用的定位因素(GPS、基站、Wi-Fi信号)的实际情况(如是否开启GPS、是否连接网络、是否有信号等)来生成相应定位依据进行定位。
用户可以设置满足自身需求的定位依据:
若用户设置GPS优先,则优先使用GPS进行定位,如果GPS定位未打开或者没有可用位置信息,且网络连接正常,定位SDK则会返回网络定位(即Wi-Fi与基站)的最优结果。为了使获得的网络定位结果更加精确,请打开手机的Wi-Fi开关。
定位SDK主要面向基于Android平台的LBS应用程序开发者,或者其他有定位需求的人员,要求具有一定Android编程经验。如果您在使用过程有任何问题,欢迎加入或通过反馈给我们。这几天自己研究了关于地手机上面开发安卓地图的问题,发现百度官方示例demo讲解百度持续定位方面还是讲解的有些不清楚,本人研究了几次之后将其弄得更详细以便于让各位方便学习,有不足之处请在评论区指出,官方示例的网址是:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/v5-0
上面的网址已经将安卓简单配置百度地图环境讲解的很详细了,再次不做赘述了,此外,可能会有人发现
1 package com.example.
3 import java.util.L
4 import java.util.T
5 import java.util.TimerT
7 import android.app.A
8 import android.content.ContentV
9 import android.content.I
10 import android.database.C
11 import android.os.B
12 import android.os.H
13 import android.os.M
14 import android.text.method.ScrollingMovementM
15 import android.util.L
16 import android.view.M
17 import android.view.MenuI
18 import android.view.V
19 import android.view.View.OnClickL
20 import android.widget.B
21 import android.widget.TextV
22 import android.widget.T
24 import com.baidu.location.BDL
25 import com.baidu.location.BDLocationL
26 import com.baidu.location.LocationC
27 import com.baidu.location.LocationClientO
28 import com.baidu.location.BDNotifyL//假如用到位置提醒功能,需要import该类
29 import com.baidu.location.LocationClientOption.LocationM
30 import com.baidu.location.P
31 import com.baidu.mapapi.SDKI
32 import com.baidu.mapapi.map.MapV
34 public class MainActivity extends Activity implements OnClickListener{
MapView mMapView = null;
public static final String TAG="mian";
StringBuffer sb = new StringBuffer(256);
public StringBuilder builder=new StringBuilder();
private Button bt1;
private TextView tv1;
private DBtools DB
boolean isOpenLocation=false;
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
DBhelper = new DBtools(this);
tv1=(TextView) findViewById(R.id.textView1);
tv1.setMovementMethod(new ScrollingMovementMethod());
bt1=(Button) findViewById(R.id.button1);
bt1.setOnClickListener(this);
mMapView = (MapView) findViewById(R.id.bmapView);
mLocationClient = new LocationClient(getApplicationContext());
//声明LocationClient类
mLocationClient.registerLocationListener( myListener );
//注册监听函数
initLocation();
private void initLocation(){
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy
63 );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
int span=0;
option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);//可选,默认false,设置是否使用gps
option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于&在北京天安门附近&
option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
mLocationClient.setLocOption(option);
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
public class MyLocationListener implements BDLocationListener {
public void onReceiveLocation(BDLocation location) {
//Receive Location
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation){// GPS定位结果
sb.append("\nspeed : ");
sb.append(location.getSpeed());// 单位:公里每小时
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\nheight : ");
sb.append(location.getAltitude());// 单位:米
sb.append("\ndirection : ");
sb.append(location.getDirection());// 单位度
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append("\ndescribe : ");
sb.append("gps定位成功");
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){// 网络定位结果
sb.append("\naddr : ");
sb.append(location.getAddrStr());
//运营商信息
sb.append("\noperationers : ");
sb.append(location.getOperators());
sb.append("\ndescribe : ");
sb.append("网络定位成功");
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
sb.append("\ndescribe : ");
sb.append("离线定位成功,离线定位结果也是有效的");
} else if (location.getLocType() == BDLocation.TypeServerError) {
sb.append("\ndescribe : ");
sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-,会有人追查原因");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("网络不同导致定位失败,请检查网络是否通畅");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
sb.append("\nlocationdescribe : ");
sb.append(location.getLocationDescribe());// 位置语义化信息
List&Poi& list = location.getPoiList();// POI数据
if (list != null) {
sb.append("\npoilist size = : ");
sb.append(list.size());
for (Poi p : list) {
sb.append("\npoi= : ");
sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
Log.i("BaiduLocationApiDem", sb.toString());
DBtools dbhelper=new DBtools(getApplicationContext());
ContentValues initialValues = new ContentValues();
initialValues.put("shijian",location.getTime());
initialValues.put("didian",location.getLatitude()+"--"+location.getLongitude());
dbhelper.open();
dbhelper.insert("path",initialValues);
dbhelper.close();
tv1.setText(sb.toString());
public void onClick(View arg0) {
Thread mytime=new Thread(new ThreadShow());
if(isOpenLocation){
mLocationClient.stop();
isOpenLocation=false;
Toast.makeText(getApplicationContext(), "开启", Toast.LENGTH_SHORT).show();
isOpenLocation=true;
//mLocationClient.start();
mytime.start();
// handler类接收数据
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
Log.i("BaiduLocationApiDem", "加以");
mLocationClient.start();
mLocationClient.requestLocation();
class ThreadShow implements Runnable {
public void run() {
// TODO Auto-generated method stub
while (isOpenLocation) {
mLocationClient.stop();
Thread.sleep(2000);
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
// System.out.println("send...");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("thread error...");
这里面关于mLocationClient.stop();mLocationClient.start();&
mLocationClient.requestLocation(); 这三个函数我有必要讲解一下,因为持续定位时这三个函数的配合使用很重要,官方文档里面解释说mLocationClient.start()函数用于开启定位,mLocationClient.requestLocation()函数用于主动触发定位SDK内部定位逻辑,个人感觉差不多,两个都会执行我的mLocationClient的所属类里面的逻辑代码,可能是我的项目就这样吧,然后是mLocationClient.stop(),此函数用于停止定位,如果持续定位的话,是需要和mLocationClient.start()函数配合使用的,具体在上面的代码里面有展示。
切记不要将mLocationClient.start()和mLocationClient.stop()一起使用,我在网上查询时好像是说一部原因,具体举一个例子吧:
1 //某段代码如果是这样的话按照逻辑韩式会将mLocationClient所属类的里面逻辑代码执行一遍,具体见MainAvtivity里面的MyLocationListener类内容,但是实际上是不会执行的
2 mLocationClient.start();
3 mLocationClient.stop();
所以在我的MainActivity里面我使用线程来一遍遍的执行start和stop函数,这样就会消除刚刚说的这种效果,最后就能够实现持续定位了。
在此给出我的布局文件配合看看
&LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.newloca.MainActivity" &
&RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"&
&com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" /&
&/RelativeLayout&
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scrollbars="vertical"
android:background="#f00"
android:text="位置信息" /&
&RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="获取位置" /&
&/RelativeLayout&
&/LinearLayout&
其他像权限什么的配置,用最开始给的官方地址里面的就行了
顺便说一下,本人是使用的安卓4.2版本开发的,手机真机调试和虚拟机调试在定位的时间间隔上面会有点误差,也不知道什么原因
阅读(...) 评论()百度地图定位
做的地图定位,用的是百度地图SDK,看了好多demo,官方的API也看了好几个版本。出的问题也各种查,也去百度地图API咨询。为了以后再有需要的时候不这么麻烦,把这几天的东西大概做个总结。就是自己看。自己看。自己看。
首先是去http://lbsyun.baidu.com/apiconsole/key申请KEY,SHA1如果是eclipse在Window&Preferences&&Build。。包名就填自己的 包名就好。。Android Studio的SHA1自己百度,我也不知道。申请好KEY后,去相关下载里把自己需要用到的开发包选中进行下载。解压。然后就是新建工程,进行配置了。配置这里我就直接Ctrl+V 百度的了。百度官方配置链接 :http://lbsyun.baidu.com/index.php?title=androidsdk/guide/buildproject 拷贝到libs目录下文件比较新的版本:链接:http://pan.baidu.com/s/1skDdrPV密码: ews2
Eclipse工程配置方法
第一步:在工程里新建libs文件夹,将开发包里的baidumapapi_vX_X_X.jar拷贝到libs根目录下,将libBaiduMapSDK_vX_X_X.so拷贝到libs\armeabi目录下(官网demo里已有这两个文件,如果要集成到自己的工程里,就需要自己添加)(自己电脑里这几个文件去F:\BaiduMap相关\拷贝到libs目录下的文件这里找。)(或者去这里下载自己分享的:链接:http://pan.baidu.com/s/1skDdrPV 密码: ews2),拷贝完成后的工程目录如下图所示;
第二步:在工程属性-& Build Path-&Libraries中选择&Add External JARs&,选定baidumapapi_vX_X_X.jar,确定后返回。
通过以上两步操作后,您就可以正常使用百度地图SDK为您提供的全部功能了。
注意:由于adt插件升级,若您使用Eclipse adt 22的话,需要对开发环境进行相应的设置,方法如下:
1. 在Eclipse 中选中工程,右键选 Properties-&Java Build Path-&Order and Export 使 Android Private Libraries处于勾选状态;
2. Project -& clean-& clean all .
Android Studio工程配置方法
第一步:在工程app/libs目录下放入baidumapapi_vX_X_X.jar包,在src/main/目录下新建jniLibs目录,工程会自动加载src目录下的so动态库,放入libBaiduMapSDK_vX_X_X_X.so如下图所示,注意jar和so的前3位版本号必须一致,并且保证使用一次下载的文件夹中的两个文件,不能不同功能的jar或so交叉使用。
so的配置也可以参考demo给出的目录结构,如下图所示,在app工程路径下,新建libs,并在libs目录下放入对应不同CPU架构的so文件。这样工程并不会自动加载libs下的so,需在gradle编译时,通过加入代码: jniLibs.srcDir 'libs' 来说明so的路径为该libs路径。
第二步:工程配置还需要把jar包集成到自己的工程中,如图上图所示,放入libs目录下。对于每个jar文件,右键-选择Add As Library,导入到工程中。对应在build.gradle生成工程所依赖的jar文件说明,如图所示:
jar的配置也可参考eclipse方法,进行以下操作:
菜单栏选择 File &&Project Structure。
在弹出的Project Structure 对话框中, 选择module, 然后点击 Dependencies 选项卡.
点击绿色的加号选择File dependency. 然后选择要添加的jar包即可 完成上边的操作后在app目录下的build.gradle文件中,会有引入的类库,如上图所示。
Android studio工程配置详细请参考官方demo。
集成地图SDK的应用,在打包混淆的时候,需要注意与地图SDK相关的方法不可被混淆。混淆方法如下:
-keep class com.baidu.** {*;}
-keep class vi.com.** {*;}
-dontwarn com.baidu.**
保证百度类不能被混淆,否则会出现网络不可用等运行时异常
说了 ,配置环境就直接粘贴的官方的。
然后是做没有地图版本的定位,开始这里接着官方文档,在Menifest清单文件中添加权限,设置service,key。
第一步:创建并配置工程(具体方法参见工程配置部分的介绍);
第二步:在AndroidManifest中添加开发密钥、所需权限等信息;
(1)在application中添加开发密钥
2)添加所需权限(这里是月份官方demo中的权限添加,和网页上的有点区别)
定位还需要在清单文件中添加一个service,因为定位SDK内部是一个SERVICE,有点绕,添加如下代码就行了:
<service android:name=&com.baidu.location.f&
android:enabled=&true&
android:process=&:remote& &
这个好像一直就是这个,SDK版本怎么变添加的service这里也是这个,好像啊。
如果为了在多个类中使用,可以将定位的相关代码写在一个继承了Application的类中,大概步骤如下:
这里只是一个思路
动态注册一个广播,来监听定位结果。注册广播在重写的onreceive方法中,利用intent获取监听到的当前位置的字符串,然后设置在TextView中。进行控件的关联及按钮的点击事件的监听。。在按钮的点击事件的监听中,开启Application。最后在写上一个进程结束后,销毁广播。
在开启的application中,首先介绍下这5个类:
1.BDLocation:封装了定位SDK的定位结果,在BDLocationListener的onReceive方法中获取。通过该类用户可以获取errorcode,位置的坐标,精度半径等信息
2.BDLocationListener:获取定位结果,获取POI信息.里面有2个onReceive方法
3.GeofenceClient:地理围栏SDK核心类 现在才发现在定位中用不到这个
4.LocationClient:定位SDK的核心类
5.LocationClientOption:该类用来设置定位SDK的定位方式
在开启的application中,首先定义一个LocationClient类型对象,再重写的OnCreate()方法中,首先给LocationClient类型的对象初始化,在调用setAK()方法,设置AK,调用registerLocatonListener()(参数为BDLocationListenter类型的对象,这里就可以使用后面的新建的继承BDLocationListenter的类的对象)来注册监听函数,不然没有办法发起网络请求。
新建一个LocationClientOption对象根据需要来设置他的各种属性。设置好之后调用locationClient的setLocOption()(参数为刚新建的LocationClientOption类型的对象)方法来为其设置参数。
设置好相关参数后,启动SDK,发起定位,分别调用start()方法和requestLocation()方法。
新建一个类实现BDLocationListener接口,重写里面的2个方法,如果参数不为空,则通过广播将获取的位置发送出去供监听者接收。当用网络定位的时候,调用方法里参数名.getAddrStr()来获取位子描述的地理位置。
这个是发送广播:
publicvoidsendBroadCast(Stringaddress){
stopLocationClient();
Intentintent=newIntent(MainActivity.LOCATION_ACTION);
intent.putExtra(&address&,address);
sendBroadcast(intent);
//发送广播,里面存有定位的结果
在广播中调用.stop()方法来停止定位SDK。
需要在Manifest清单文件中添加一个service和添加好多个权限。
具体代码如下:
* BDLocation: 封装了定位SDK的定位结果,在BDLocationListener的onReceive方法中获取。通过该类用户可以获取error code,位置的坐标,精度半径等信息
* BDLocationListener: 获取定位结果,获取POI信息. 里面有2个onReceive方法
* GeofenceClient:地理围栏SDK核心类
* LocationClient: 定位SDK的核心类
* LocationClientOption:该类用来设置定位SDK的定位方式
public class MyApplication extends Application {
private String AK = &0vm2PvFx8WLzhn3kPemOeA7KbwAPAz8S&; // 和应用证书和包名绑定绑定的密钥
private static MyApplication instance =
private LocationClient locationClient =
private MyBDLocationListener myBDLocationListener = new MyBDLocationListener();
public void onCreate() {
instance =
locationClient = new LocationClient(this);
locationClient.setAK(AK); // 设置AK,高版本SDK在Manifest中进行设置。
locationClient.registerLocationListener(myBDLocationListener);
// 注册监听函数,当没有注册监听函数时,无法发起网络请求。
super.onCreate();
public static MyApplication getInstance() {
* 设置相关参数
private void setLocationClientOption() {
LocationClientOption locationClientOption = new LocationClientOption();
locationClientOption.setOpenGps(true); // 打开GPS
locationClientOption.setCoorType(&bd09ll&); // 返回结果返回百度经纬度坐标系
// 这个属性一定要设置 不然找不到服务就无法定位。不过在高版本中是设置在Manifest中的
locationClientOption.setServiceName(&com.baidu.location.service_v2.9&);
locationClientOption.setScanSpan(3000);// 这个设置定位间隔,即3秒定位一次,不设置默认为定位1次
locationClientOption.setPoiExtraInfo(true); // 设置是否需要POI的电话地址等详细信息
locationClientOption.setAddrType(&all&);// 设置是否要返回地址信息,默认为无地址信息。String 值为 all时,表示返回地址信息。
locationClientOption.setPoiNumber(10); // 设置POI个数
locationClientOption.disableCache(true); // 设置是否使用缓存定位
// setLocOption:设置参数
// 方法:public void setLocOption ( LocationClientOption )
// 配置定位SDK,参数为LocationClientOption类
locationClient.setLocOption(locationClientOption);
* 发起定位
public void requestLocationInfo() {
setLocationClientOption();
if (locationClient != null && !locationClient.isStarted()) {
locationClient.start(); // 启动定位SDK 这里其实可以把上面这几行代码封装在一个方法里,这样就可以多次方便的调用了。不过这里就自己看,就不改了
if (locationClient != null && locationClient.isStarted()) {
locationClient.requestLocation(); // 发起定位,异步获取当前位置
* 发起离线定位
public void requestLocationInfo2() {
setLocationClientOption();
if (locationClient != null && !locationClient.isStarted()) {
locationClient.start(); // 启动定位SDK
if (locationClient != null && locationClient.isStarted()) {
locationClient.requestOfflineLocation(); // 发起定位,异步获取当前位置
* 监听函数,有更新位置的时候,格式化成字符串,输出到屏幕中 BDLocationListener:获取定位结果,获取POI信息。 2个方法:
* public void onReceiveLocation ( BDLocation location ); public void
* onReceivePoi(BDLocation poiLocation);
* BDLocationListener在较高的版本中里面就一个ofReceive方法,这里是2个方法,一个是地理位置,一个是兴趣点
* poiLocation.getAddrStr():获取文字描述的地址(反地理编码)(网络定位)
public class MyBDLocationListener implements BDLocationListener {
// 获取定位结果的接口。用户自己实现这个接口后,监听定位结果
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (location == null) {
sendBroadCast(&定位失败!&);
StringBuffer sb = new StringBuffer(256);
sb.append(&time : &);
sb.append(location.getTime());
sb.append(&\nerror code : &);
sb.append(location.getLocType());
sb.append(&\nlatitude : &);
sb.append(location.getLatitude());
sb.append(&\nlontitude : &);
sb.append(location.getLongitude());
sb.append(&\nradius : &);
sb.append(location.getRadius());
sb.append(&\n定位方法: &);
sb.append(location.getLocType());
if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
sb.append(&\nspeed : &);
sb.append(location.getSpeed());// 单位:公里每小时
sb.append(&\nsatellite : &);
sb.append(location.getSatelliteNumber());
sb.append(&\nheight : &);
sb.append(location.getAltitude());// 单位:米
sb.append(&\naddr : &);
sb.append(location.getAddrStr());
sb.append(&\ndescribe : &);
sb.append(&gps定位成功&);
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
sb.append(&\naddr : &);
sb.append(location.getAddrStr());
sb.append(&\ndescribe : &);
sb.append(&网络定位成功&);
sendBroadCast(location.getAddrStr() + sb.toString());
Toast.makeText(getApplicationContext(), &在线定位&,
Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
sb.append(&\ndescribe : &);
sb.append(&离线定位成功,离线定位结果也是有效的&);
geoPoint = new GeoPoint(latitudeOff, longitudeOff);
mkSearch.reverseGeocode(geoPoint);
Toast.makeText(getApplicationContext(), &离线定位&,
Toast.LENGTH_SHORT).show();
} else if (location.getLocType() == BDLocation.TypeServerError) {
sb.append(&\ndescribe : &);
sb.append(&服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-,会有人追查原因&);
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append(&\ndescribe : &);
sb.append(&网络不同导致定位失败,请检查网络是否通畅&);
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append(&\ndescribe : &);
sb.append(&无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机&);
MainActivity.LATITUDE = location.getLatitude();
MainActivity.LONGITUDE = location.getLongitude();
Log.i(&BaiduLocationApiDem&, sb.toString());
if (location.getAddrStr() != null) {
sendBroadCast(location.getAddrStr() + sb.toString());
sendBroadCast(&未知地区&);
// 获取POI信息的接口。用户自己实现这个接口后,监听poi的相关结果
public void onReceivePoi(BDLocation poi) {
// TODO Auto-generated method stub
if (poi == null) {
sendBroadCast(&定位失败!&);
sendBroadCast(poi.getAddrStr());
* 得到结果,发送广播
* @param address
* 结果,是一个字符串
public void sendBroadCast(String address) {
stopLocationClient();
Intent intent = new Intent(MainActivity.LOCATION_ACTION);
intent.putExtra(&address&, address);
sendBroadcast(intent);
// 发送广播,里面存有定位的结果
* 停止定位
public void stopLocationClient() {
if (locationClient != null && locationClient.isStarted()) {
// 关闭定位SDK,调用stop之后,设置的参数LocationClientOption仍然保留
locationClient.stop();
然后再需要调用的类中调用方法,开启广播接受结果就可以了。以下是在主类中的代码:
public class MainActivity extends Activity implements OnClickListener {
private Button btnOnL
private Button btnOffL
private TextView tvH
private TextView tvL
private TextView textV
public static double LATITUDE = 0; // 这个就是保存纬度和经度
public static double LONGITUDE = 0;
private BroadcastReceiver broadcastR
public static String LOCATION_ACTION = &location_action&;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
registerBroadCastReceiver();
btnOnLine.setOnClickListener(this);
btnOffLine.setOnClickListener(this);
* 动态的注册一个广播,来监听定位结果
private void registerBroadCastReceiver() {
broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String address = intent.getStringExtra(&address&);
if (address == null) {
tvLocation.setText(&无位置信息&);
tvLocation.setText(address);
textView.setText(&纬度:& + LATITUDE + &\n经度:& + LONGITUDE);
IntentFilter intentFilter = new IntentFilter(LOCATION_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
// 控件的关联
private void init() {
btnOnLine = (Button) findViewById(R.id.test_btn_online);
btnOffLine = (Button) findViewById(R.id.test_btn_offline);
tvHint = (TextView) findViewById(R.id.test_tv_hint);
tvLocation = (TextView) findViewById(R.id.test_tv_location);
textView = (TextView) findViewById(R.id.test_tv_xinxi);
// 重写监听事件
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.test_btn_online:
tvHint.setVisibility(View.VISIBLE);
tvLocation.setText(&正在定位......&);
textView.setText(& &);
MyApplication.getInstance().requestLocationInfo();
case R.id.test_btn_offline:
// if (tvHint.VISIBLE != View.VISIBLE) {
tvHint.setVisibility(View.VISIBLE);
tvLocation.setText(&正在定位......&);
MyApplication.getInstance().requestLocationInfo2();
// 进程结束后,销毁广播
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(broadcastReceiver);
不过这里我有个问题就是你要把Menifest清单文件中的Application改成上面那个你继承Application的类。如下:
android:name=&com.cmmr.location.test.MyApplication&
加上这个来引用你自己写的类
这个怎么在这里添加图片啊 不知道怎么搞了 ,反正就是下面那个Application 的图片
没有地图的版本大概就是这个样子吧,当然了 ,如果不想使用广播,且定位需求就在一个类的话,可以将上面MyApplication类中的广播去掉,剩下的大概就挑挑写在想要实现定位的类中,然后再监听返回结果的方法里处理结果,在合适的地方给地图Start和stop就可以了。其实到现在定位SDK的版本和以前改动不是太大,改动以我的理解大概是这些:KEY和service在menifist中配置,监听里改为一个方法,option中可以设置定位的模式。我的感觉改动就是这些。
(链接: http://pan.baidu.com/s/1nvlbyIX 密码: i4wb)这个源码中我贱嗖嗖的将定位结果如果文字信息多的话以跑马灯的形式显示。本来想把有地图的版本也在这里写的 ,想想算了 感觉还是分分类,那个就在重新写一个吧

我要回帖

更多关于 定位别人手机号码位置 的文章

 

随机推荐