1. 程式人生 > >android高德地圖定位功能的實現

android高德地圖定位功能的實現

<1>先去高德開放平臺去申請開發者賬號,並且建立應用,獲取API Key
<2>在清單檔案中配置key,其中value是應用的key值

 <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="952d22d6f6e916e38fbe830d4517ca12" />

配置許可權

    <!-- 用於進行網路定位 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
/>
<!-- 用於訪問GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 用於獲取運營商資訊,用於支援提供運營商資訊相關的介面 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 用於訪問wifi網路資訊,wifi資訊會用於進行網路定位 --> <uses-permission
android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 用於獲取wifi的獲取許可權,wifi資訊會用來進行網路定位 --> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!-- 用於訪問網路,網路定位需要上網 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 用於讀取手機當前的狀態 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 用於寫入快取資料到擴充套件儲存卡 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 用於申請呼叫A-GPS模組 --> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <!-- 用於申請獲取藍芽資訊進行室內定位 --> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<3>在xml檔案中新增地圖控制元件


<com.amap.api.maps.MapView
    android:id="@+id/location_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</com.amap.api.maps.MapView>

<4>展示定位地圖

public class LocationActivity extends AppCompatActivity {
    public AMapLocationClient client=null;
    private AMapLocationClientOption option=null;
    private MapView mapView;
    private AMap map;
    private TextView tvAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        initView(savedInstanceState);

    }

    private void initListener() {
        client.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if(aMapLocation!=null){
                    if(aMapLocation.getErrorCode()==0){
                        aMapLocation.getLocationType();//獲取當前結果來源,如網路定位.GPS定位
                        double lat=aMapLocation.getLatitude();//獲取緯度
                        double lon=aMapLocation.getLongitude();//獲取經度
                        aMapLocation.getAccuracy();//獲取精度資訊

//                        option.setOnceLocation(true);
                        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Date date=new Date(aMapLocation.getTime());//定位時間
                        LatLng latlon=new LatLng(lat,lon);
                        MarkerOptions marker=new MarkerOptions();
                        map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlon,15));//顯示在指定位置
                        marker.position(latlon);
                        marker.title("當前位置");
                        marker.visible(true);
                        BitmapDescriptor bitmapDescriptor= BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.point));
                        marker.icon(bitmapDescriptor);
                        map.addMarker(marker);
                        tvAdd.setText("當前位置:"+aMapLocation.getAddress()+"         "+format.format(date));
                    }else{
                        Toast.makeText(LocationActivity.this,"定位失敗",Toast.LENGTH_SHORT).show();
                    }
                }
                    map.setMyLocationEnabled(true);
            }
        });
    }

    private void initLocation() {

        option=new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setNeedAddress(true);
        option.setInterval(60000);
        client.setLocationOption(option);
        client.startLocation();

    }

    private void initView(Bundle savedInstanceState) {
        mapView = ((MapView) findViewById(R.id.location_view));
        tvAdd = ((TextView) findViewById(R.id.location_tv));
        //此方法必須重寫
        mapView.onCreate(savedInstanceState);
        client=new AMapLocationClient(getApplicationContext());
        initListener();
        map=mapView.getMap();
        map.setMapType(AMap.MAP_TYPE_NORMAL);
        initLocation();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        client.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}