GoogleMap不存在,SouGou占有率不高,BaiDu niao性差,所以肯定实现地图首选高德啦..
官方文档地址: http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key
注册账号,创建对应的应用.
这里基本就是填资料啦,没什么难的地方,只不过创建应用之后设置key的时候,要获取SHA1,
我的获取方法:androidstudio3.0,利用Terminal工具:
根据签名文件分别获取debug和release的SHA1,直接贴主要命令行吧:
D:\A_Git3\XHBC>cd D:\JDK8\bin
D:\JDK8\bin>keytool -v -list -keystore C:\Users\zjp\.android\debug.keystore
***************** WARNING WARNING WARNING *****************
条目类型: PrivateKeyEntry
证书指纹:
MD5: 0A:02:44:CC:D2:65:A************62:ED:A3:DD:34
SHA1: 79:BC:05:39:AA:35:CD:7************F:33:CA
SHA256: 9E:F0:B1:1C:8B:59:C6:2A:17:A************7:FE:E7:EB:93
签名算法名称: SHA1
版本: 1
*******************************************
D:\JDK8\bin>keytool -v -list -keystore D:\SIGN\x**.jks
条目类型: PrivateKeyEntry
证书指纹:
MD5: 04:8E:1C:84:39:DF:78************:15:E9:F6
SHA1: 23:64:74:64:E0:80:A************:73:10:9E:8A:3D:57:EA
SHA256: 2A:5F:BE:E7:94:2B:DF:C************:BC:42:57:51:4D:EA
签名算法名称: SHA256withRSA
*******************************************
官方文档地址: http://lbs.amap.com/api/android-location-sdk/guide/create-project/android-studio-create-project
这里基本照着文档走就没问题,注意问题如下:
官方文档地址: http://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation
此处注意2D和3D地图因为添加的jar包不同其MapView路径也是不同的,如下
2D:
3D:
此处参考这篇博文,写的很详细了,如侵权请告知我立即删改,此处我在这个哥们写的基础上加上一点我自己的理解吧: https://www.jianshu.com/p/c3dc0cea0a2d
public class MainActivity extends AppCompatActivity implements LocationSource, AMapLocationListener {
private MapView mapView; //地图控件
private AMap aMap; //地图对象
//定位需要的声明,初始化的配置
private AMapLocationClient mLocationClient = null; //发起定位
private AMapLocationClientOption mLocationOption = null; //参数设置
private OnLocationChangedListener mListener = null; //监听器
//标识,用于判断是否只显示一次定位信息和用户重新定位
private boolean isFirstLoc = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.map);
//这句是重点,如果是自定义BaseActivity的要注意重写onCreate()方法
mapView.onCreate(savedInstanceState);
//获取地图对象
aMap = mapView.getMap();
//设置显示定位按钮 并且可以点击
UiSettings settings = aMap.getUiSettings();
//设置定位监听
aMap.setLocationSource(this);
// 是否显示定位按钮
settings.setMyLocationButtonEnabled(true);
// 是否可触发定位并显示定位层
aMap.setMyLocationEnabled(true);
//定位的小图标
MyLocationStyle myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.point_map));
myLocationStyle.radiusFillColor(android.R.color.transparent);
myLocationStyle.strokeColor(android.R.color.transparent);
aMap.setMyLocationStyle(myLocationStyle);
//开始定位
initLocation();
}
/**
* 定位
*/
private void initLocation() {
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);
//设置是否只定位一次,默认为false
mLocationOption.setOnceLocation(false);
//设置是否强制刷新WIFI,默认为强制刷新
mLocationOption.setWifiActiveScan(true);
//设置是否允许模拟位置,默认为false,不允许模拟位置
mLocationOption.setMockEnable(false);
//设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(2000);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation();
}
//定位回调函数
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
//定位成功回调信息,设置相关消息
amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见官方定位类型表
amapLocation.getLatitude(); //获取纬度
amapLocation.getLongitude(); //获取经度
amapLocation.getAccuracy(); //获取精度信息
amapLocation.getCity(); //城市信息
// 如果不设置标志位,此时再拖动地图时,它会不断将地图移动到当前的位置
if (isFirstLoc) {
//设置缩放级别
aMap.moveCamera(CameraUpdateFactory.zoomTo(17));
//将地图移动到定位点
aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude())));
//点击定位按钮 能够将地图的中心移动到定位点
mListener.onLocationChanged(amapLocation);
//添加图钉
aMap.addMarker(getMarkerOptions(amapLocation));
//获取定位信息
StringBuffer buffer = new StringBuffer();
buffer.append(amapLocation.getCountry() + "" + amapLocation.getProvince() + "" + amapLocation.getCity() + "" + amapLocation.getProvince() + "" + amapLocation.getDistrict() + "" + amapLocation.getStreet() + "" + amapLocation.getStreetNum());
Toast.makeText(getApplicationContext(), buffer.toString(), Toast.LENGTH_LONG).show();
isFirstLoc = false;
}
} else {
//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
Toast.makeText(getApplicationContext(), "定位失败", Toast.LENGTH_LONG).show();
}
}
}
//自定义一个图钉,并且设置图标,当我们点击图钉时,显示设置的信息
private MarkerOptions getMarkerOptions(AMapLocation amapLocation) {
//设置图钉选项
MarkerOptions options = new MarkerOptions();
//图标
options.icon(BitmapDescriptorFactory.fromResource(R.mipmap.fire));
//位置
options.position(new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude()));
StringBuffer buffer = new StringBuffer();
buffer.append(amapLocation.getCountry() + "" + amapLocation.getProvince() + "" + amapLocation.getCity() + "" + amapLocation.getDistrict() + "" + amapLocation.getStreet() + "" + amapLocation.getStreetNum());
//标题
options.title(buffer.toString());
//子标题
options.snippet("这是我设置的标题");
//设置多少帧刷新一次图片资源
options.period(60);
return options;
}
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
}
@Override
public void deactivate() {
mListener = null;
}
@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();
}
}