1、自动定位
我们先看一下根据自己的地理位置实现定位
1.首先初始化要用到的组件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**
* 初始化组件
*/
private
void
initView() {
btnLocationBack = (Button) findViewById(R.id.btn_location_back);
btnLocationBack.setOnClickListener(
this
);
btnLocationOk = (Button) findViewById(R.id.btn_location_ok);
btnLocationOk.setOnClickListener(
this
);
mapViewLocation = (MapView) findViewById(R.id.mapview_location);
lvLocNear = (ListView) findViewById(R.id.lv_location_nearby);
nearList =
new
ArrayList<PoiInfo>();
adapter =
new
LocNearAddressAdapter(context, nearList, isSelected);
lvLocNear.setAdapter(adapter);
}
|
2.初始化LocationClient类,该类需要在主线程中声明
1
2
3
4
5
6
7
|
public
LocationClient mLocationClient =
null
;
public
BDLocationListener myListener =
new
MyLocationListener();
public
void
onCreate() {
mLocationClient =
new
LocationClient(getApplicationContext());
//声明LocationClient类
mLocationClient.registerLocationListener( myListener );
//注册监听函数
}
|
3.配置定位SDK参数
设置定位参数包括:定位模式(高精度定位模式,低功耗定位模式和仅用设备定位模式),返回坐标类型,是否打开GPS,是否返回地址信息、位置语义化信息、POI信息等等。
LocationClientOption类,该类用来设置定位SDK的定位方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private
void
initLocation(){
LocationClientOption option =
new
LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy
);
//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType(
"bd09ll"
);
//可选,默认gcj02,设置返回的定位结果坐标系
int
span=
1000
;
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
);
//可选,默认false,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认杀死
option.SetIgnoreCacheException(
false
);
//可选,默认false,设置是否收集CRASH信息,默认收集
option.setEnableSimulateGps(
false
);
//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
mLocationClient.setLocOption(option);
}
|
4.实现BDLocationListener接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
/**
* 监听函数,有新位置的时候,格式化成字符串,输出到屏幕中
*/
public
class
MyLocationListenner
implements
BDLocationListener {
@Override
public
void
onReceiveLocation(BDLocation location) {
if
(location ==
null
) {
return
;
}
Log.d(
"map"
,
"On location change received:"
+ location);
Log.d(
"map"
,
"addr:"
+ location.getAddrStr());
if
(progressDialog !=
null
) {
progressDialog.dismiss();
}
if
(lastLocation !=
null
) {
if
(lastLocation.getLatitude() == location.getLatitude() && lastLocation.getLongitude() == location.getLongitude()) {
Log.d(
"map"
,
"same location, skip refresh"
);
// mMapView.refresh(); //need this refresh?
return
;
}
}
lastLocation = location;
mBaiduMap.clear();
mCurrentLantitude = lastLocation.getLatitude();
mCurrentLongitude = lastLocation.getLongitude();
Log.e(
">>>>>>>"
, mCurrentLantitude +
","
+ mCurrentLongitude);
LatLng llA =
new
LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
CoordinateConverter converter =
new
CoordinateConverter();
converter.coord(llA);
converter.from(CoordinateConverter.CoordType.COMMON);
LatLng convertLatLng = converter.convert();
OverlayOptions ooA =
new
MarkerOptions().position(convertLatLng).icon(BitmapDescriptorFactory
.fromResource(R.drawable.icon_marka))
.zIndex(
4
).draggable(
true
);
mCurrentMarker = (Marker) mBaiduMap.addOverlay(ooA);
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng,
16
.0f);
mBaiduMap.animateMapStatus(u);
new
Thread(
new
Runnable() {
@Override
public
void
run() {
searchNeayBy();
}
}).start();
}
public
void
onReceivePoi(BDLocation poiLocation) {
if
(poiLocation ==
null
) {
return
;
}
}
}
|
这里接受到的BDLocation中包含好多参数,相信总有一个对你有用的。
2、根据经纬度定位
这种方法不需要自动定位,就是根据经纬度来显示地图上的位置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/*
* 显示经纬度的位置
* */
private
void
showMap(
double
latitude,
double
longtitude, String address) {
// sendButton.setVisibility(View.GONE);
LatLng llA =
new
LatLng(latitude, longtitude);
CoordinateConverter converter =
new
CoordinateConverter();
converter.coord(llA);
converter.from(CoordinateConverter.CoordType.COMMON);
LatLng convertLatLng = converter.convert();
OverlayOptions ooA =
new
MarkerOptions().position(convertLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marka))
.zIndex(
4
).draggable(
true
);
markerA = (Marker) (mBaiduMap.addOverlay(ooA));
u = MapStatusUpdateFactory.newLatLngZoom(convertLatLng,
16
.0f);
mBaiduMap.animateMapStatus(u);
new
Thread(
new
Runnable() {
@Override
public
void
run() {
searchNeayBy();
}
}).start();
}
|
3、获取周边地理位置
最后看一下怎么获取周边的地理位置,这里需要用到SDK中的一个类PoiNearbySearchOption,我们可以看一下类参考:
- PoiNearbySearchOption keyword(java.lang.String key)
- 检索关键字
- PoiNearbySearchOption location(LatLng location)
- 检索位置
- PoiNearbySearchOption pageCapacity(int pageCapacity)
- 设置每页容量,默认为每页10条
- PoiNearbySearchOption pageNum(int pageNum)
- 分页编号
- PoiNearbySearchOption radius(int radius)
- 设置检索的半径范围
- PoiNearbySearchOption sortType(PoiSortType sortType)
- 搜索结果排序规则,可选,默认
这里是它的一些方法,我们可以看到我们只需要设置一下关键字、周边位置半径、检索位置、排序规则、分页号、每页数量等。然后我们实现OnGetPoiSearchResultListener这个接口,获取周边地理位置结果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
/**
* 搜索周边地理位置
*/
private
void
searchNeayBy() {
PoiNearbySearchOption option =
new
PoiNearbySearchOption();
option.keyword(
"写字楼"
);
option.sortType(PoiSortType.distance_from_near_to_far);
option.location(
new
LatLng(mCurrentLantitude, mCurrentLongitude));
if
(radius !=
0
) {
option.radius(radius);
}
else
{
option.radius(
1000
);
}
option.pageCapacity(
20
);
mPoiSearch.searchNearby(option);
}
/*
* 接受周边地理位置结果
* @param poiResult
*/
@Override
public
void
onGetPoiResult(PoiResult poiResult) {
if
(poiResult !=
null
) {
if
(poiResult.getAllPoi()!=
null
&&poiResult.getAllPoi().size()>
0
){
nearList.addAll(poiResult.getAllPoi());
if
(nearList !=
null
&& nearList.size() >
0
) {
for
(
int
i =
0
; i < nearList.size(); i++) {
isSelected.put(i,
false
);
}
}
Message msg =
new
Message();
msg.what =
0
;
handler.sendMessage(msg);
}
}
}
|
获取完数据之后更新适配器显示周边位置就OK了,最后再实现一个小小的功能,就是点击列表中的每个位置,显示位置的小图标根据位置的改变而改变
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/**
* 周边地理位置列表点击事件
*/
lvLocNear.setOnItemClickListener(
new
AdapterView.OnItemClickListener() {
@Override
public
void
onItemClick(AdapterView<?> adapterView, View view,
int
i,
long
l) {
adapter.setSelected(i);
adapter.notifyDataSetChanged();
PoiInfo ad = (PoiInfo) adapter.getItem(i);
u = MapStatusUpdateFactory.newLatLng(ad.location);
mBaiduMap.animateMapStatus(u);
if
(!isLoc) {
mCurrentMarker.setPosition(ad.location);
}
else
{
markerA.setPosition(ad.location);
}
}
});
|