android---高德地图(2)---定位-显示小蓝点
2016-04-27 20:00
495 查看
配置工程:
1.从网站下载并解压得到定位包“Android_Location_V1.xx.jar“。
2.开发工程中新建“libs”文件夹,将定位包拷贝到 libs 的根目录下。拷贝完成后的工程目录(以 V1.0.4 为例)如图所示:
(!)添加 用户appkey和权限已经在(1)中介绍过。
1.从网站下载并解压得到定位包“Android_Location_V1.xx.jar“。
2.开发工程中新建“libs”文件夹,将定位包拷贝到 libs 的根目录下。拷贝完成后的工程目录(以 V1.0.4 为例)如图所示:
(!)添加 用户appkey和权限已经在(1)中介绍过。
public
class
MainActivity
extends
Activity
implements
LocationSource,AMapLocationListener {
private
AMap aMap;
private
MapView mapView;
private
OnLocationChangedListener mListener;
private
LocationManagerProxy mAMapLocationManager;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.locationsource_activity);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
init();
}
/**
* 初始化AMap对象
*/
private
void
init() {
if
(aMap ==
null
) {
aMap = mapView.getMap();
setUpMap();
}
}
private
void
setUpMap() {
Map.setLocationSource(
this
);
// 设置定位监听
aMap.getUiSettings().setMyLocationButtonEnabled(
true
);
// 设置默认定位按钮是否显示
aMap.setMyLocationEnabled(
true
);
// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
// 设置定位的类型为定位模式:定位(AMap.LOCATION_TYPE_LOCATE)、跟随(AMap.LOCATION_TYPE_MAP_FOLLOW)
// 地图根据面向方向旋转(AMap.LOCATION_TYPE_MAP_ROTATE)三种模式
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
}
/**
* 此方法需存在
*/
@Override
protected
void
onResume() {
super
.onResume();
mapView.onResume();
}
/**
* 此方法需存在
*/
@Override
protected
void
onPause() {
super
.onPause();
mapView.onPause();
deactivate();
}
/**
* 此方法需存在
*/
@Override
protected
void
onDestroy() {
super
.onDestroy();
mapView.onDestroy();
}
/**
* 此方法已经废弃
*/
@Override
public
void
onLocationChanged(Location location) {
}
/**
* 定位成功后回调函数
*/
@Override
public
void
onLocationChanged(AMapLocation amapLocation) {
if
(mListener !=
null
&& amapLocation !=
null
) {
if
(amapLocation.getAMapException().getErrorCode() ==
0
) {
mListener.onLocationChanged(amapLocation);
// 显示系统小蓝点
}
}
}
/**
* 激活定位
*/
@Override
public
void
activate(OnLocationChangedListener listener) {
mListener = listener;
if
(mAMapLocationManager ==
null
) {
mAMapLocationManager = LocationManagerProxy.getInstance(
this
);
//此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
//注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求
//在定位结束后,在合适的生命周期调用destroy()方法
//其中如果间隔时间为-1,则定位只定一次
mAMapLocationManager.requestLocationData(
LocationProviderProxy.AMapNetwork,
60
*
1000
,
10
,
this
);
}
}
/**
* 停止定位
*/
@Override
public
void
deactivate() {
mListener =
null
;
if
(mAMapLocationManager !=
null
) {
mAMapLocationManager.removeUpdates(
this
);
mAMapLocationManager.destroy();
}
mAMapLocationManager =
null
;
}
}
相关文章推荐
- Android Glide图片加载框架图片变色变绿解决方法
- Android AssetManager 简读<2>
- android---高德地图(3)点击获得目的经纬度,地理编码
- android---高德地图(4)路线规划
- android---高德地图(5)导航界面(语音播报)实现
- android---简单语音合成
- android---自定义折线图
- android---读取SD卡内容并显示
- android---重写textview自适应屏幕分辨率,压缩字体大小
- android---重写EditText自适应屏幕分辨率,压缩字体大小
- android---世界时区列表
- Android 批量上传图片进度回调
- android---对SharePreferences的封装
- android---瀑布流gridview
- android---防止ListView数据错乱的方法
- android---自定义数字选择器
- android---ScrollView中嵌套ListView
- Android ImageButton,Button,CheckBox等抢了原有View的点击事件 点击事件冲突问题
- android---后台代发短信
- android---service的启动,和activity的通信