百度Android定位SDK实现获取当前经纬度及位置
来源:互联网 发布:淘宝美工职责 编辑:程序博客网 时间:2024/05/17 08:44
url: http://blog.csdn.net/hewei0241/article/details/24044871
转载自http://www.open-open.com/lib/view/open1346982366162.html 程序有点过时但是 改改还能用
使用Android自带的LocationManager和Location获取位置的时候,经常会有获取的location为null的情况,并且操作起来也不是很方便,在这个Demo里我使用了百度地图API中的定位SDK,可以一次性获取当前位置经纬度以及详细地址信息,还可以获取周边POI信息,同时可以设定位置通知点,当到达某一位置时,发出通知信息等方式来告知用户。jar包下载以及官方文档请参照:百度定位SDK,前提是需要注册百度开发者账号。
下面来看看定位的基本原理,目前,定位SDK可以通过GPS、基站、Wifi信号进行定位。基本定位流程如下图所示,当应用程序向定位SDK发起定位请求时,定位SDK会根据当前的GPS、基站、Wifi信息生成相对应的定位依据。然后定位SDK会根据定位依据来进行定位。如果需要,定位SDK会向定位服务器发送网络请求。定位服务器会根据请求的定位依据推算出对应的坐标位置,然后根据用户的定制信息,生成定位结果返回给定位SDK。
到官方下载jar文件后添加到工程,工程目录截图如下:
注意要把locSDK_2.4.jar添加到当天工程,右键jar文件-Build path-Add to。。。
上代码
布局文件:
01
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
03
android:layout_width
=
"fill_parent"
04
android:layout_height
=
"fill_parent"
05
android:orientation
=
"vertical"
>
06
07
<
Button
08
android:id
=
"@+id/btn_start"
09
android:layout_width
=
"fill_parent"
10
android:layout_height
=
"wrap_content"
11
android:layout_marginTop
=
"20dp"
12
android:text
=
"Start"
/>
13
14
<
TextView
15
android:id
=
"@+id/tv_loc_info"
16
android:layout_width
=
"fill_parent"
17
android:layout_height
=
"wrap_content"
18
android:textSize
=
"18sp"
/>
19
20
</
LinearLayout
>
配置文件:
01
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02
<
manifest
xmlns:android
=
"http://schemas.android.com/apk/res/android"
03
package
=
"com.ericssonlabs"
04
android:versionCode
=
"1"
05
android:versionName
=
"1.0"
>
06
07
<
uses-sdk
android:minSdkVersion
=
"8"
/>
08
09
<
permission
android:name
=
"android.permission.BAIDU_LOCATION_SERVICE"
>
10
</
permission
>
11
12
<
uses-permission
android:name
=
"android.permission.BAIDU_LOCATION_SERVICE"
>
13
</
uses-permission
>
14
<
uses-permission
android:name
=
"android.permission.ACCESS_COARSE_LOCATION"
>
15
</
uses-permission
>
16
<
uses-permission
android:name
=
"android.permission.ACCESS_FINE_LOCATION"
>
17
</
uses-permission
>
18
<
uses-permission
android:name
=
"android.permission.ACCESS_WIFI_STATE"
>
19
</
uses-permission
>
20
<
uses-permission
android:name
=
"android.permission.ACCESS_NETWORK_STATE"
>
21
</
uses-permission
>
22
<
uses-permission
android:name
=
"android.permission.CHANGE_WIFI_STATE"
>
23
</
uses-permission
>
24
<
uses-permission
android:name
=
"android.permission.READ_PHONE_STATE"
>
25
</
uses-permission
>
26
<
uses-permission
android:name
=
"android.permission.WRITE_EXTERNAL_STORAGE"
>
27
</
uses-permission
>
28
<
uses-permission
android:name
=
"android.permission.INTERNET"
/>
29
<
uses-permission
android:name
=
"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
>
30
</
uses-permission
>
31
<
uses-permission
android:name
=
"android.permission.READ_LOGS"
>
32
</
uses-permission
>
33
34
<
application
35
android:icon
=
"@drawable/ic_launcher"
36
android:label
=
"@string/app_name"
>
37
<
activity
38
android:name
=
".LocationDemoActivity"
39
android:label
=
"@string/app_name"
>
40
<
intent-filter
>
41
<
action
android:name
=
"android.intent.action.MAIN"
/>
42
43
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
44
</
intent-filter
>
45
</
activity
>
46
47
<
service
48
android:name
=
"com.baidu.location.f"
49
android:enabled
=
"true"
50
android:permission
=
"android.permission.BAIDU_LOCATION_SERVICE"
51
android:process
=
":remote"
>
52
<
intent-filter
>
53
<
action
android:name
=
"com.baidu.location.service_v2.4"
/>
54
</
intent-filter
>
55
</
service
>
56
</
application
>
57
58
</
manifest
>
实现代码:
001
public
class
LocationDemoActivity
extends
Activity {
002
private
TextView locationInfoTextView =
null
;
003
private
Button startButton =
null
;
004
private
LocationClient locationClient =
null
;
005
private
static
final
int
UPDATE_TIME =
5000
;
006
private
static
int
LOCATION_COUTNS =
0
;
007
008
@Override
009
public
void
onCreate(Bundle savedInstanceState) {
010
super
.onCreate(savedInstanceState);
011
setContentView(R.layout.main);
012
013
locationInfoTextView = (TextView)
this
.findViewById(R.id.tv_loc_info);
014
startButton = (Button)
this
.findViewById(R.id.btn_start);
015
016
017
locationClient =
new
LocationClient(
this
);
018
//设置定位条件
019
LocationClientOption option =
new
LocationClientOption();
020
option.setOpenGps(
true
);
//是否打开GPS
021
option.setCoorType(
"bd09ll"
);
//设置返回值的坐标类型。
022
option.setPriority(LocationClientOption.NetWorkFirst);
//设置定位优先级
023
option.setProdName(
"LocationDemo"
);
//设置产品线名称。强烈建议您使用自定义的产品线名称,方便我们以后为您提供更高效准确的定位服务。
024
option.setScanSpan(UPDATE_TIME);
//设置定时定位的时间间隔。单位毫秒
025
locationClient.setLocOption(option);
026
027
//注册位置监听器
028
locationClient.registerLocationListener(
new
BDLocationListener() {
029
030
@Override
031
public
void
onReceiveLocation(BDLocation location) {
032
// TODO Auto-generated method stub
033
if
(location ==
null
) {
034
return
;
035
}
036
StringBuffer sb =
new
StringBuffer(
256
);
037
sb.append(
"Time : "
);
038
sb.append(location.getTime());
039
sb.append(
"\nError code : "
);
040
sb.append(location.getLocType());
041
sb.append(
"\nLatitude : "
);
042
sb.append(location.getLatitude());
043
sb.append(
"\nLontitude : "
);
044
sb.append(location.getLongitude());
045
sb.append(
"\nRadius : "
);
046
sb.append(location.getRadius());
047
if
(location.getLocType() == BDLocation.TypeGpsLocation){
048
sb.append(
"\nSpeed : "
);
049
sb.append(location.getSpeed());
050
sb.append(
"\nSatellite : "
);
051
sb.append(location.getSatelliteNumber());
052
}
else
if
(location.getLocType() == BDLocation.TypeNetWorkLocation){
053
sb.append(
"\nAddress : "
);
054
sb.append(location.getAddrStr());
055
}
056
LOCATION_COUTNS ++;
057
sb.append(
"\n检查位置更新次数:"
);
058
sb.append(String.valueOf(LOCATION_COUTNS));
059
locationInfoTextView.setText(sb.toString());
060
}
061
062
@Override
063
public
void
onReceivePoi(BDLocation location) {
064
}
065
066
});
067
068
startButton.setOnClickListener(
new
OnClickListener() {
069
070
@Override
071
public
void
onClick(View v) {
072
if
(locationClient ==
null
) {
073
return
;
074
}
075
if
(locationClient.isStarted()) {
076
startButton.setText(
"Start"
);
077
locationClient.stop();
078
}
else
{
079
startButton.setText(
"Stop"
);
080
locationClient.start();
081
/*
082
*当所设的整数值大于等于1000(ms)时,定位SDK内部使用定时定位模式。
083
*调用requestLocation( )后,每隔设定的时间,定位SDK就会进行一次定位。
084
*如果定位SDK根据定位依据发现位置没有发生变化,就不会发起网络请求,
085
*返回上一次定位的结果;如果发现位置改变,就进行网络请求进行定位,得到新的定位结果。
086
*定时定位时,调用一次requestLocation,会定时监听到定位结果。
087
*/
088
locationClient.requestLocation();
089
}
090
}
091
});
092
093
}
094
095
@Override
096
protected
void
onDestroy() {
097
super
.onDestroy();
098
if
(locationClient !=
null
&& locationClient.isStarted()) {
099
locationClient.stop();
100
locationClient =
null
;
101
}
102
}
103
104
105
}
来看看最后实现效果,点击Start后进入位置监听状态,根据设置的监听时间间隔进行定位,如果位置有变化则进行位置更新,同时显示了检测位置更新的次数,如果开启了GPS,则获取到卫星后,进行GPS定位:
设置位置提醒的功能我这里就没实现了,感兴趣的可以参考开发指南
- 百度Android定位SDK实现获取当前经纬度及位置
- 百度Android定位SDK实现获取当前经纬度及位置
- 百度Android定位SDK实现获取当前经纬度及位置
- 百度Android定位SDK实现获取当前经纬度及位置
- 百度Android定位SDK实现获取当前经纬度及位置
- 百度定位SDK实现获取当前经纬度及位置
- 百度定位SDK实现获取当前经纬度及位置
- 百度定位SDK实现获取当前经纬度及位置
- (转载)百度Android定位SDK实现获取当前经纬度及位置
- Android利用百度定位SDK获取当前城市位置
- Android定位获取当前经纬度
- Android百度定位获取经纬度
- iOS百度地图 定位,获取当前经纬度
- 百度api获取经纬度及详细位置
- Android定位获取经纬度并显示位置
- 使用百度地图SDK实现定位功能,定位你当前的位置
- 通过百度定位sdk获取实时位置
- 通过百度定位sdk获取实时位置
- BPMN规范中的三种视图
- 与MQ通讯的完整JAVA程序
- 设计模式大全
- Model1与Model2
- iOS修改app的名称(icon下面显示的名称)
- 百度Android定位SDK实现获取当前经纬度及位置
- 2014学习总结
- Unity之Shader在模型上渲染积雪效果-三
- jQuery.Autocomplete实现自动完成功能(详解)
- [leetcode 15] 3Sum
- 代码小知识
- JAVA程序设计(20)----- 查询信息的数据库代码
- GDB寄存器和内存
- 2014年过去与未来