欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Studio Location位置_高德地图

程序员文章站 2022-05-15 17:16:39
...

高德平台

获取文件创建项目
Android Studio Location位置_高德地图
Activity

package com.example.day9;

import android.annotation.SuppressLint;
import android.location.Location;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;

import java.security.Provider;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private MapView mapView;
    private AMap map;
    private AMapLocationClient client;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        mapView = findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        initmap();
    }

    public void initmap(){
        map = mapView.getMap();
        map.setMapType(AMap.MAP_TYPE_SATELLITE);

        client = new AMapLocationClient(getApplicationContext());
        client.setLocationListener(locationListener);

        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setNeedAddress(true);
        option.setMockEnable(true);
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//高精度位置

        client.setLocationOption(option);
        client.startLocation();
    }

    public AMapLocationListener locationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation.getErrorCode() == 0){
                double latitude = aMapLocation.getLatitude();
                double longitude = aMapLocation.getLongitude();
                LatLng latLng = new LatLng(latitude, longitude);//坐标对象
                map.clear();
                MarkerOptions options = new MarkerOptions();
                options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
                options.position(latLng);//定位图标要显示的位置
                options.draggable(true);

                map.addMarker(options);//给地图添加标记
//                CameraUpdate update = CameraUpdateFactory.changeLatLng(latLng);//更新定位范围
//                map.moveCamera(update);//显示
            }else {
                Toast.makeText(Main2Activity.this, "完犊子", Toast.LENGTH_SHORT).show();
            }
        }
    };

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

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

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

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">

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

</LinearLayout>