1、要实现高德地图的定位与显示,首先要下载高德地图的SDK以及高德地图定位的SDK :

下载地址:http://lbs.amap.com/api/android-sdk/download/
http://lbs.amap.com/api/android-location-sdk/download/
2. 注册账号后到控制台应用管理 —–>添加应用 —-> 添加key 获取到自己的key值 。
获取key的具体方法在官网上: http://lbs.amap.com/
Android Studio具体操作文档:https://lbs.amap.com/dev/key/app
3、通过解压官方zip得到 .jar文件,放到应用下的libs文件夹中,手动添加到依赖库中(右键Add As Lirbrary) 。
4、如果你的地图SDK是3D的需要在main中创建 jniLibs 文件夹并把你解压出来的其他文件如 x86,放到此处 。
3,4 两步可参考:https://lbs.amap.com/api/android-location-sdk/guide/create-project/android-studio-create-project
配置如下:
5、开发环境已经配置好了,接下来就是敲代码了。
在应用的“ AndroidManifest.xml ”文件的 application 标签内 添加key(自己注册的应用key)和 Service
<!-- value 值:自己注册的应用key值 --><meta-data android:name="com.amap.api.v2.apikey"android:value="自己注册的应用key值" /><!-- 在application标签中声明service组件:--><service android:name="com.amap.api.location.APSService"></service>
添加权限
<!--用于访问网络,网络定位需要上网--><uses-permission android:name="android.permission.INTERNET" /><!--用于申请调用A-GPS模块--><uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/><!--用于进行网络定位--><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网络信息--><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.READ_PHONE_STATE" /><!--写入扩展存储,向扩展卡写入数据,用于写入缓存定位数据--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!--用于申请获取蓝牙信息进行室内定位--><uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
acticity_main.xml 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="标准地图" android:layout_weight="1" android:id="@+id/biaoZhun" android:layout_width="0dp" android:layout_height="wrap_content" /><Button android:text="卫星地图" android:layout_weight="1" android:id="@+id/weiXing" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:text="夜间模式" android:id="@+id/yeJian" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_marginTop="18dp" android:layout_width="match_parent" android:layout_height="match_parent"></com.amap.api.maps.MapView>
</LinearLayout>
MainActivity代码
package 你的包名;import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import java.text.SimpleDateFormat;
import java.util.Date;/**** 地图图层:http://lbs.amap.com/api/android-sdk/guide/create-map/set-maptype/* * MAP_TYPE_NAVI 导航地图* MAP_TYPE_NIGHT 夜景地图* MAP_TYPE_NORMAL 白昼地图(即普通地图)* MAP_TYPE_SATELLITE 卫星图** 集成SDK注意事项:* 1. 设备运行App 之前切记打开GPS导航定位功能;* 2. 集成SDK前,首先配备好 key值;配置 key值时,SHA1 和 应用包名 一定填对。**/
public class MainActivity extends AppCompatActivity implements LocationSource, AMapLocationListener {private static String TAG = "MainActivity";private AMap aMap; //AMap是地图对象private MapView mapView; //声明AMapLocationClient类对象,定位发起端private AMapLocationClient mLocationClient = null; //声明mLocationOption对象,定位参数public AMapLocationClientOption mLocationOption = null; //声明mListener对象,定位监听器private OnLocationChangedListener mListener = null; //标识,用于判断是否只显示一次定位信息和用户重新定位private boolean isFirstLoc = true;private Button yeJian;private Button biaoZhun;private Button weiXing;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取地图控件引用mapView = (MapView) findViewById(R.id.map);yeJian = (Button)findViewById(R.id.yeJian);weiXing = (Button)findViewById(R.id.weiXing);biaoZhun = (Button)findViewById(R.id.biaoZhun);//夜间模式地图yeJian.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this, "正在切换至 夜景地图 ,请稍候~ ", Toast.LENGTH_LONG).show();aMap.setMapType(AMap.MAP_TYPE_NIGHT);//夜景地图,aMap是地图控制器对象。}});//卫星模式地图weiXing.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this, "正在切换至 卫星地图 ,请稍候~ ", Toast.LENGTH_LONG).show();aMap.setMapType(AMap.MAP_TYPE_SATELLITE);// 设置卫星地图模式,aMap是地图控制器对象。}});//标准模式地图biaoZhun.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this, "正在切换至 标准地图 ,请稍候~ ", Toast.LENGTH_LONG).show();aMap.setMapType(AMap.MAP_TYPE_NORMAL);//设置白昼地图(即普通地图)}});//在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),实现地图生命周期管理mapView.onCreate(savedInstanceState);if (aMap == null) {aMap = mapView.getMap();//设置显示定位按钮 并且可以点击UiSettings settings = aMap.getUiSettings();aMap.setLocationSource(this);//设置了定位的监听// 是否显示定位按钮settings.setMyLocationButtonEnabled(true);aMap.setMyLocationEnabled(true);//显示定位层并且可以触发定位,默认是flase}//开始定位location();}private void location() {//初始化定位mLocationClient = new AMapLocationClient(getApplicationContext());//设置定位回调监听mLocationClient.setLocationListener(this);//初始化定位参数mLocationOption = new AMapLocationClientOption();//设置定位模式为Hight_Accuracy高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//设置是否返回地址信息(默认返回地址信息)mLocationOption.setNeedAddress(true);//设置是否只定位一次,默认为falsemLocationOption.setOnceLocation(false);//设置是否强制刷新WIFI,默认为强制刷新mLocationOption.setWifiActiveScan(true);//设置是否允许模拟位置,默认为false,不允许模拟位置mLocationOption.setMockEnable(false);//设置定位间隔,单位毫秒,默认为2000msmLocationOption.setInterval(2000);//给定位客户端对象设置定位参数mLocationClient.setLocationOption(mLocationOption);//显示实时路况图层,aMap是地图控制器对象。aMap.setTrafficEnabled(true);//启动定位mLocationClient.startLocation();}@Overrideprotected void onDestroy() {super.onDestroy();//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理mapView.onDestroy();mLocationClient.stopLocation();//停止定位 销毁定位客户端。mLocationClient.onDestroy();}@Override protected void onResume() {super.onResume();//在activity执行onResume时执行mMapView.onResume (),实现地图生命周期管理mapView.onResume();}@Overrideprotected void onPause() {super.onPause();//在activity执行onPause时执行mMapView.onPause (),实现地图生命周期管理mapView.onPause();}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);//在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),实现地图生命周期管理mapView.onSaveInstanceState(outState);}@Overridepublic void onLocationChanged(AMapLocation aMapLocation) {if (aMapLocation != null) {if (aMapLocation.getErrorCode() == 0) {//定位成功回调信息,设置相关消息aMapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见官方定位类型表aMapLocation.getLatitude();//获取纬度aMapLocation.getLongitude();//获取经度aMapLocation.getAccuracy();//获取精度信息SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date(aMapLocation.getTime());df.format(date);//定位时间aMapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。aMapLocation.getCountry();//国家信息aMapLocation.getProvince();//省信息aMapLocation.getCity();//城市信息aMapLocation.getDistrict();//城区信息aMapLocation.getStreet();//街道信息aMapLocation.getStreetNum();//街道门牌号信息aMapLocation.getCityCode();//城市编码aMapLocation.getAdCode();// 地区编码//3.实现高德地图定位功能,并在控制台打印坐标信息;Log.e(TAG, "详细定位信息 :" + aMapLocation.getAddress());Log.e(TAG, "全称定位信息 :" + aMapLocation.getCountry() +""+ aMapLocation.getProvince() +""+ aMapLocation.getCity() +""+aMapLocation.getDistrict() +""+ aMapLocation.getStreet() +""+ aMapLocation.getStreetNum());Log.e(TAG, "经度:" + aMapLocation.getLongitude() + " 纬度:" + aMapLocation.getLatitude());Toast.makeText(MainActivity.this, "当前位置信息 :" + aMapLocation.getAddress() + " 经度:" + aMapLocation.getLongitude() + " 纬度:" + aMapLocation.getLatitude(), Toast.LENGTH_LONG).show();// 如果不设置标志位,此时再拖动地图时,它会不断将地图移动到当前的位置if (isFirstLoc) {//设置缩放级别aMap.moveCamera(CameraUpdateFactory.zoomTo(17));//将地图移动到定位点aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));//点击定位按钮 能够将地图的中心移动到定位点mListener.onLocationChanged(aMapLocation);// 添加图钉 Marker,资源使用应用图标,设置当前地图显示为当前位置aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()), 19));MarkerOptions markerOptions = new MarkerOptions();markerOptions.position(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()));markerOptions.visible(true);BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));markerOptions.icon(bitmapDescriptor);aMap.addMarker(markerOptions);//获取定位信息StringBuffer buffer = new StringBuffer();buffer.append(aMapLocation.getCountry() +""+ aMapLocation.getProvince() +""+ aMapLocation.getCity() +""+ aMapLocation.getProvince() +""+aMapLocation.getDistrict() + "" + aMapLocation.getStreet() + "" + aMapLocation.getStreetNum());Toast.makeText(MainActivity.this, buffer.toString(), Toast.LENGTH_LONG).show();isFirstLoc = false;}} else {//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。Log.e(TAG, "location Error, ErrCode: " + aMapLocation.getErrorCode() + " , errInfo: " + aMapLocation.getErrorInfo());Toast.makeText(MainActivity.this, "定位失败, ErrCode: " + aMapLocation.getErrorCode() + " , errInfo: " + aMapLocation.getErrorInfo(), Toast.LENGTH_LONG).show();}}}@Overridepublic void activate(OnLocationChangedListener onLocationChangedListener) {mListener = onLocationChangedListener;}@Overridepublic void deactivate() {mListener = null;}
}
到这里就简单实现高德地图的定位与显示~ 希望本篇文章对你有所帮助。