Android通过高德地图进行定位
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
packagecom.example.dingwei;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importcom.amap.api.location.AMapLocation;
importcom.amap.api.location.AMapLocationClient;
importcom.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
importcom.amap.api.location.AMapLocationListener;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.location.Criteria;
importandroid.location.Location;
importandroid.location.LocationListener;
importandroid.location.LocationManager;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.widget.TextView;
public class MainActivity extends Activity {
//声明AMapLocationClient类对象
publicAMapLocationClientmLocationClient = null;
//声明定位回调监听器
publicAMapLocationListenermLocationListener =new MyListener();
//声明mLocationOption对象
publicAMapLocationClientOptionmLocationOption = null;
privateLocationManagerlocationManager;
privateTextViewlocationText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.activity_main);
locationText=(TextView) findViewById(R.id.location);
set();
/*locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria=new Criteria();
//设置规则
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//设置电量
criteria.setPowerRequirement(Criteria.POWER_LOW) ;
String provider=locationManager.getBestProvider(criteria, true);
//String provider=locationManager.getAllProviders().get(1);
locationManager.requestLocationUpdates(provider, 3000, 100, new MyListener());*/ }
public void set(){
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听
mLocationClient.setLocationListener(mLocationListener);
//初始化定位参数
mLocationOption = new AMapLocationClientOption();
//设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);
//设置是否只定位一次,默认为false
mLocationOption.setOnceLocation(false);
//设置是否强制刷新WIFI,默认为强制刷新
mLocationOption.setWifiActiveScan(true);
//设置是否允许模拟位置,默认为false,不允许模拟位置
mLocationOption.setMockEnable(false);
//设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(2000);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation();
}
private class MyListener implements AMapLocationListener{
public void onLocationChanged(AMapLocationamapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
//定位成功回调信息,设置相关消息
amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
amapLocation.getLatitude();//获取纬度
amapLocation.getLongitude();//获取经度