原文:https://blog.csdn.net/uyy203/article/details/55213357
一、基礎(chǔ)地圖部分
1.獲取APPKey
在高德地圖注冊(cè)開發(fā)者賬號(hào)裳朋,然后創(chuàng)建應(yīng)用病线,使用SHA1和PackageName得到Key
創(chuàng)建應(yīng)用的鏈接:http://lbs.amap.com/
創(chuàng)建應(yīng)用 輸入你的應(yīng)用名稱 選擇你的應(yīng)用類型 點(diǎn)擊確定
點(diǎn)擊確定之后得到這么一個(gè)東西點(diǎn)擊添加新key 輸入完畢后再下方點(diǎn)擊提交 獲取SHA1值以及應(yīng)用包名見下方
獲取SHA1值方法:
首先使用cmd 打開dos窗口
輸入cd .android 切換到 .android目錄
使用keytool工具獲取簽名的SHA1值 輸入命令:keytool -list -v -keystore debug.keystore 秘鑰口令輸入 android 或者直接回車
這里的指紋整數(shù)有MD5、SHA1鲤嫡、SHA256 這樣就拿到了上面需要的SHA1值
獲取應(yīng)用包名:
打開你的gradle文件
這就是你的應(yīng)用包名
提交之后得到的AppKey:
2.下載SDK
下載地址:http://lbs.amap.com/api/android-sdk/down/
定位SDK下載地址:http://lbs.amap.com/api/android-location-sdk/download/
3.導(dǎo)入項(xiàng)目
把下載下來的壓縮包解壓送挑,里面的jar包放到項(xiàng)目的libs目錄下
然后Add As Library
集成高德地圖需要用到.so文件 解壓得到的.so文件 目錄里面就是所需的.so文件
在main目錄下創(chuàng)建jniLibs目錄 注意是main目錄 將解壓得到的.so文件連帶目錄都copy到j(luò)niLibs目錄下
4.配置清單文件
在AndroidManifest.xml里面添加你的AppKey
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="99ac75ad4aa47d9b865cee7c3ef31a89"/>
在AndroidManifest.xml中添加用戶權(quán)限
//地圖包、搜索包需要的基礎(chǔ)權(quán)限
<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" />
//定位包暖眼、導(dǎo)航包需要的額外權(quán)限(注:基礎(chǔ)權(quán)限也需要)
<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.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
配置Service
<service android:name="com.amap.api.location.APSService"/>
5.布局文件:
在布局文件中添加地圖控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="demo.gaode.com.gaodemapdemo.ShowMapActivity">
<!-- 引入布局文件 -->
<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" />
</RelativeLayout>
6.正式開始寫代碼了
1.基礎(chǔ)地圖顯示
效果圖
初始化地圖控件 管理地圖控件生命周期
package demo.gaode.com.gaodemapblog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.amap.api.maps.MapView;
public class ShowMapActivity extends AppCompatActivity {
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_map);
//初始化地圖控件
mapView = (MapView) findViewById(R.id.map);
//必須要寫
mapView.onCreate(savedInstanceState);
}
/**
* 方法必須重寫
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必須重寫
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
/**
* 方法必須重寫
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必須重寫
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
2.定位功能實(shí)現(xiàn)
效果圖以及Log
效果圖和基礎(chǔ)地圖的效果圖沒什么區(qū)別惕耕,因?yàn)槲壹葲]有設(shè)置中心點(diǎn)也沒有設(shè)置marker,但是我獲取到了位置诫肠,那么定位完畢后添加一個(gè)中心點(diǎn)司澎,一個(gè)marker就可以完成基礎(chǔ)地圖+定位+marker的效果了
先把定位的所有代碼貼出來
package demo.gaode.com.gaodemapblog;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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.MapView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ShowMapActivity extends Activity {
private MapView mapView;
private AMap aMap;
//聲明AMapLocationClient類對(duì)象
public AMapLocationClient mLocationClient = null;
//聲明定位回調(diào)監(jiān)聽器
public AMapLocationListener mLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
//定位成功回調(diào)信息,設(shè)置相關(guān)消息
amapLocation.getLocationType();//獲取當(dāng)前定位結(jié)果來源栋豫,如網(wǎng)絡(luò)定位結(jié)果挤安,詳見定位類型表
amapLocation.getLatitude();//獲取緯度
amapLocation.getLongitude();//獲取經(jīng)度
amapLocation.getAccuracy();//獲取精度信息
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(amapLocation.getTime());
df.format(date);//定位時(shí)間
amapLocation.getAddress();//地址,如果option中設(shè)置isNeedAddress為false丧鸯,則沒有此結(jié)果蛤铜,網(wǎng)絡(luò)定位結(jié)果中會(huì)有地址信息,GPS定位不返回地址信息。
amapLocation.getCountry();//國(guó)家信息
amapLocation.getProvince();//省信息
amapLocation.getCity();//城市信息
amapLocation.getDistrict();//城區(qū)信息
amapLocation.getStreet();//街道信息
amapLocation.getStreetNum();//街道門牌號(hào)信息
amapLocation.getCityCode();//城市編碼
amapLocation.getAdCode();//地區(qū)編碼
amapLocation.getAoiName();//獲取當(dāng)前定位點(diǎn)的AOI信息
lat = amapLocation.getLatitude();
lon = amapLocation.getLongitude();
Log.v("pcw","lat : "+lat+" lon : "+lon);
} else {
//顯示錯(cuò)誤信息ErrCode是錯(cuò)誤碼围肥,errInfo是錯(cuò)誤信息剿干,詳見錯(cuò)誤碼表。
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
}
}
}
};
//聲明mLocationOption對(duì)象
public AMapLocationClientOption mLocationOption = null;
private double lat;
private double lon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_map);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);//必須要寫
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
//設(shè)置定位回調(diào)監(jiān)聽
mLocationClient.setLocationListener(mLocationListener);
init();
}
/**
* * 初始化AMap對(duì)象
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
}
setUpMap();
}
private void setUpMap() {
//初始化定位參數(shù)
mLocationOption = new AMapLocationClientOption();
//設(shè)置定位模式為高精度模式虐先,Battery_Saving為低功耗模式怨愤,Device_Sensors是僅設(shè)備模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//設(shè)置是否返回地址信息(默認(rèn)返回地址信息)
mLocationOption.setNeedAddress(true);
//設(shè)置是否只定位一次,默認(rèn)為false
mLocationOption.setOnceLocation(false);
//設(shè)置是否強(qiáng)制刷新WIFI,默認(rèn)為強(qiáng)制刷新
mLocationOption.setWifiActiveScan(true);
//設(shè)置是否允許模擬位置,默認(rèn)為false蛹批,不允許模擬位置
mLocationOption.setMockEnable(false);
//設(shè)置定位間隔,單位毫秒,默認(rèn)為2000ms
mLocationOption.setInterval(2000);
//給定位客戶端對(duì)象設(shè)置定位參數(shù)
mLocationClient.setLocationOption(mLocationOption);
//啟動(dòng)定位
mLocationClient.startLocation();
}
/**
* 方法必須重寫
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必須重寫
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mLocationClient.stopLocation();//停止定位
}
/**
* 方法必須重寫
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必須重寫
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
mLocationClient.onDestroy();//銷毀定位客戶端撰洗。
}
}
這里面有一個(gè)地方千萬要記住,一定要在AndroidManifest.xml中注冊(cè)廣播 否則定位會(huì)失敗
<service android:name="com.amap.api.location.APSService"/>
下面來講講這段代碼里面幾個(gè)組成:
初始化定位
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
//設(shè)置定位回調(diào)監(jiān)聽
mLocationClient.setLocationListener(mLocationListener);
初始化AMap對(duì)象
/**
* * 初始化AMap對(duì)象
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
}
setUpMap();
}
配置定位參數(shù)并開始定位
/**
* 配置定位參數(shù)
*/
private void setUpMap() {
//初始化定位參數(shù)
mLocationOption = new AMapLocationClientOption();
//設(shè)置定位模式為高精度模式腐芍,Battery_Saving為低功耗模式差导,Device_Sensors是僅設(shè)備模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//設(shè)置是否返回地址信息(默認(rèn)返回地址信息)
mLocationOption.setNeedAddress(true);
//設(shè)置是否只定位一次,默認(rèn)為false
mLocationOption.setOnceLocation(false);
//設(shè)置是否強(qiáng)制刷新WIFI,默認(rèn)為強(qiáng)制刷新
mLocationOption.setWifiActiveScan(true);
//設(shè)置是否允許模擬位置,默認(rèn)為false猪勇,不允許模擬位置
mLocationOption.setMockEnable(false);
//設(shè)置定位間隔,單位毫秒,默認(rèn)為2000ms
mLocationOption.setInterval(2000);
//給定位客戶端對(duì)象設(shè)置定位參數(shù)
mLocationClient.setLocationOption(mLocationOption);
//啟動(dòng)定位
mLocationClient.startLocation();
}
在onStop里面停止定位
@Override
protected void onStop() {
super.onStop();
mLocationClient.stopLocation();//停止定位
}
在onDestory方法里面銷毀定位
/**
* 方法必須重寫
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
mLocationClient.onDestroy();//銷毀定位客戶端设褐。
}
設(shè)置定位監(jiān)聽,在監(jiān)聽中可以獲取定位的一系列數(shù)據(jù)
/**
* 聲明定位回調(diào)監(jiān)聽器
*/
public AMapLocationListener mLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
//定位成功回調(diào)信息泣刹,設(shè)置相關(guān)消息
amapLocation.getLocationType();//獲取當(dāng)前定位結(jié)果來源助析,如網(wǎng)絡(luò)定位結(jié)果,詳見定位類型表
amapLocation.getLatitude();//獲取緯度
amapLocation.getLongitude();//獲取經(jīng)度
amapLocation.getAccuracy();//獲取精度信息
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(amapLocation.getTime());
df.format(date);//定位時(shí)間
amapLocation.getAddress();//地址椅您,如果option中設(shè)置isNeedAddress為false外冀,則沒有此結(jié)果,網(wǎng)絡(luò)定位結(jié)果中會(huì)有地址信息掀泳,GPS定位不返回地址信息雪隧。
amapLocation.getCountry();//國(guó)家信息
amapLocation.getProvince();//省信息
amapLocation.getCity();//城市信息
amapLocation.getDistrict();//城區(qū)信息
amapLocation.getStreet();//街道信息
amapLocation.getStreetNum();//街道門牌號(hào)信息
amapLocation.getCityCode();//城市編碼
amapLocation.getAdCode();//地區(qū)編碼
amapLocation.getAoiName();//獲取當(dāng)前定位點(diǎn)的AOI信息
lat = amapLocation.getLatitude();
lon = amapLocation.getLongitude();
Log.v("pcw","lat : "+lat+" lon : "+lon);
} else {
//顯示錯(cuò)誤信息ErrCode是錯(cuò)誤碼,errInfo是錯(cuò)誤信息员舵,詳見錯(cuò)誤碼表脑沿。
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
}
}
}
};
3.設(shè)置中心點(diǎn)以及marker
效果圖
有了效果圖 那么我把基礎(chǔ)地圖+定位+marker的代碼貼出來
package demo.gaode.com.gaodemapblog;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
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.MapView;
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;
public class ShowMapActivity extends Activity {
private MapView mapView;
private AMap aMap;
//聲明AMapLocationClient類對(duì)象
public AMapLocationClient mLocationClient = null;
/**
* 聲明定位回調(diào)監(jiān)聽器
*/
public AMapLocationListener mLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
//定位成功回調(diào)信息,設(shè)置相關(guān)消息
amapLocation.getLocationType();//獲取當(dāng)前定位結(jié)果來源马僻,如網(wǎng)絡(luò)定位結(jié)果庄拇,詳見定位類型表
amapLocation.getLatitude();//獲取緯度
amapLocation.getLongitude();//獲取經(jīng)度
amapLocation.getAccuracy();//獲取精度信息
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(amapLocation.getTime());
df.format(date);//定位時(shí)間
amapLocation.getAddress();//地址,如果option中設(shè)置isNeedAddress為false韭邓,則沒有此結(jié)果措近,網(wǎng)絡(luò)定位結(jié)果中會(huì)有地址信息,GPS定位不返回地址信息仍秤。
amapLocation.getCountry();//國(guó)家信息
amapLocation.getProvince();//省信息
amapLocation.getCity();//城市信息
amapLocation.getDistrict();//城區(qū)信息
amapLocation.getStreet();//街道信息
amapLocation.getStreetNum();//街道門牌號(hào)信息
amapLocation.getCityCode();//城市編碼
amapLocation.getAdCode();//地區(qū)編碼
amapLocation.getAoiName();//獲取當(dāng)前定位點(diǎn)的AOI信息
lat = amapLocation.getLatitude();
lon = amapLocation.getLongitude();
Log.v("pcw", "lat : " + lat + " lon : " + lon);
Log.v("pcw", "Country : " + amapLocation.getCountry() + " province : " + amapLocation.getProvince() + " City : " + amapLocation.getCity() + " District : " + amapLocation.getDistrict());
// 設(shè)置當(dāng)前地圖顯示為當(dāng)前位置
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 19));
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(new LatLng(lat, lon));
markerOptions.title("當(dāng)前位置");
markerOptions.visible(true);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.location_marker));
markerOptions.icon(bitmapDescriptor);
aMap.addMarker(markerOptions);
} else {
//顯示錯(cuò)誤信息ErrCode是錯(cuò)誤碼熄诡,errInfo是錯(cuò)誤信息可很,詳見錯(cuò)誤碼表诗力。
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
}
}
}
};
//聲明mLocationOption對(duì)象
public AMapLocationClientOption mLocationOption = null;
private double lat;
private double lon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_map);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);//必須要寫
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
//設(shè)置定位回調(diào)監(jiān)聽
mLocationClient.setLocationListener(mLocationListener);
init();
}
/**
* * 初始化AMap對(duì)象
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
}
setUpMap();
}
/**
* 配置定位參數(shù)
*/
private void setUpMap() {
//初始化定位參數(shù)
mLocationOption = new AMapLocationClientOption();
//設(shè)置定位模式為高精度模式,Battery_Saving為低功耗模式,Device_Sensors是僅設(shè)備模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//設(shè)置是否返回地址信息(默認(rèn)返回地址信息)
mLocationOption.setNeedAddress(true);
//設(shè)置是否只定位一次,默認(rèn)為false
mLocationOption.setOnceLocation(false);
//設(shè)置是否強(qiáng)制刷新WIFI苇本,默認(rèn)為強(qiáng)制刷新
mLocationOption.setWifiActiveScan(true);
//設(shè)置是否允許模擬位置,默認(rèn)為false袜茧,不允許模擬位置
mLocationOption.setMockEnable(false);
//設(shè)置定位間隔,單位毫秒,默認(rèn)為2000ms
mLocationOption.setInterval(2000);
//給定位客戶端對(duì)象設(shè)置定位參數(shù)
mLocationClient.setLocationOption(mLocationOption);
//啟動(dòng)定位
mLocationClient.startLocation();
}
/**
* 方法必須重寫
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必須重寫
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mLocationClient.stopLocation();//停止定位
}
/**
* 方法必須重寫
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必須重寫
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
mLocationClient.onDestroy();//銷毀定位客戶端。
}
}
代碼實(shí)際上沒什么難度瓣窄,也不多笛厦,下面我把設(shè)置marker的代碼貼出來
// 設(shè)置當(dāng)前地圖顯示為當(dāng)前位置
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 19));
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(new LatLng(lat, lon));
markerOptions.title("當(dāng)前位置");
markerOptions.visible(true);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.location_marker));
markerOptions.icon(bitmapDescriptor);
aMap.addMarker(markerOptions);
好了,到這里 基礎(chǔ)地圖+定位+marker就做出來了