高德定位

<!-- 引入布局文件 -->
    <com.amap.api.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"

        android:layout_height="match_parent" />


//地图包、搜索包需要的基础权限


    <uses-permission android:name="android.permission.INTERNET" />


    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />


    <uses-permission android:name="android.permission.READ_PHONE_STATE" />


    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


    //定位包、导航包需要的额外权限(注:基础权限也需要)


    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />


    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />


    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />



<meta-data 
            android:name="com.amap.api.v2.apikey"
            android:value="87c0150fd8a38899af746c1a44efbf1a"
            /


package com.example.gaode02;


import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;


import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;


public class MainActivity extends Activity implements AMapLocationListener {
//声明变量
    private MapView mapView;
    private AMap aMap;
    private LocationManagerProxy mLocationManagerProxy;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManagerProxy = LocationManagerProxy.getInstance(this);
       //此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
       //注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求
       //在定位结束后,在合适的生命周期调用destroy()方法     
       //其中如果间隔时间为-1,则定位只定一次
       mLocationManagerProxy.requestLocationData(
               LocationProviderProxy.AMapNetwork,-1, 15, this);
       mLocationManagerProxy.setGpsEnable(false);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);// 必须要写
aMap = mapView.getMap();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mLocationManagerProxy.destroy();
}


@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub

}


@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}


@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}


@Override
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub
if(arg0 != null && arg0.getAMapException().getErrorCode() == 0){
            //获取位置信息
            //Double geoLat = arg0.getLatitude();
            //Double geoLng = arg0.getLongitude();
            Log.e("TGA","地址"+arg0.toString());
            Log.e("TGA","地址"+arg0.getAMapException().getErrorCode());
        }
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值