android 高德定位
今天所分享的是一个简单的高德定位,就是简单的获取当前的位置,可能高德导航要好玩点,但是目前暂未有那方面的需求,所以就只是用到了高德的一个定位。其实高德文档已经写得差不多了,但是自己做了以后就当做一个笔记吧,高德api地址是http://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation
//高德地图定位
compile 'com.amap.api:location:latest.integration'
二、清单文件AndroidMainfest.xml
<!--权限-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!--高德地图定位-->
<meta-data android:name="com.amap.api.v2.apikey" android:value="8049c3fe80541356b00a6055f0ef9789">
</meta-data>
<service android:name="com.amap.api.location.APSService"></service>
三、MainActivity
1、声明对象
//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;
//声明定位回调监听器
public AMapLocationListener mLocationListener = new MyAMapLocationListener();
//声明AMapLocationClientOption对象
public AMapLocationClientOption mLocationOption = null;
2、高德定位方法
// 高德定位
public void getPositioning() {
//初始化定位
mLocationClient = new AMapLocationClient(mContext);
//设置定位回调监听
mLocationClient.setLocationListener(mLocationListener);
//初始化AMapLocationClientOption对象
mLocationOption = new AMapLocationClientOption();
//获取一次定位结果:
//该方法默认为false。
mLocationOption.setOnceLocation(true);
//获取最近3s内精度最高的一次定位结果:
//设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。
mLocationOption.setOnceLocationLatest(true);
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);
//设置是否允许模拟位置,默认为true,允许模拟位置
mLocationOption.setMockEnable(true);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//启动定位
mLocationClient.startLocation();
}
3、高德定位回调
// 高德定位回调
class MyAMapLocationListener implements AMapLocationListener{
@Override
public void onLocationChanged(final AMapLocation aMapLocation) {
new Thread(new Runnable() {
@Override
public void run() {
Location location = new Location();
location.setLatitude(aMapLocation.getLatitude());
location.setLongitude(aMapLocation.getLongitude());
location.setAddress(aMapLocation.getAddress());
location.setCountry(aMapLocation.getCountry());
location.setCity(aMapLocation.getCity());
location.setDistrict(aMapLocation.getDistrict());
location.setStreet(aMapLocation.getStreet());
location.setStreetNum(aMapLocation.getStreetNum());
location.setCityCode(aMapLocation.getCityCode());
location.setAdCode(aMapLocation.getAdCode());
location.setPoiName(aMapLocation.getPoiName());
location.setAoiName(aMapLocation.getAoiName());
location.setErrorCode(aMapLocation.getErrorCode());
Gson gson = new Gson();
String json = gson.toJson(location);
}
}).start();
}
}
4、3中Location实体类
package com.iwiteks.PalmarTourism.bean;
/**
* Created by me on 2018/2/2.
*/
public class Location {
private double latitude;
private double longitude;
private String address;
private String country;
private String city;
private String district;
private String street;
private String streetNum;
private String cityCode;
private String adCode;
private String poiName;
private String aoiName;
private Integer errorCode;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getStreetNum() {
return streetNum;
}
public void setStreetNum(String streetNum) {
this.streetNum = streetNum;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public String getAdCode() {
return adCode;
}
public void setAdCode(String adCode) {
this.adCode = adCode;
}
public String getPoiName() {
return poiName;
}
public void setPoiName(String poiName) {
this.poiName = poiName;
}
public String getAoiName() {
return aoiName;
}
public void setAoiName(String aoiName) {
this.aoiName = aoiName;
}
public Integer getErrorCode() {
return errorCode;
}
public void setErrorCode(Integer errorCode) {
this.errorCode = errorCode;
}
}
5、至此,3中json就是返回的定位信息json字符串信息