1.创建应用获取KEY
- 创建应用
- 填写应用相关的信息
- 获取SHA1值
- 获取KEY值
2.Android studio 项目配置,集成SDK
- 配置Gradle,通过build.gradle进行sdk的配置
- 配置方式,添加依赖
implementation 'com.amap.api:location:4.6.0'
implementation 'com.amap.api:map2d:5.2.0'
implementation 'com.amap.api:search:6.9.0'
-
注意事项
1、3D地图 SDK 和导航 SDK,5.0.0 版本以后全面支持多平台 so 库(armeabi、armeabi-v7a、arm64-v8a、x86、x86_64),开发者可以根据需要选择。同时还需要注意的是:如果您涉及到新旧版本更替请移除旧版本的 so 库之后替换新版本 so 库到工程中。2、navi导航SDK 5.0.0以后版本包含了3D地图SDK,所以请不要同时引入 map3d 和 navi SDK。
3、如果build失败提示com.amap.api:XXX:X.X.X 找不到,请确认拼写及版本号是否正确,如果访问不到jcenter可以切换为maven仓库尝试一下。
4、依照上述方法引入 SDK 以后,不需要在libs文件夹下导入对应SDK的 so 和 jar 包,会有冲突。
3.实现定位与POI搜索
- 在AndroidManifest.xml配置key
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="请输入您的用户Key"/>
……
</application>
- 配置权限
//地图包、搜索包需要的基础权限
<!--允许程序打开网络套接字-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允许程序设置内置sd卡的写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许程序获取网络状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许程序访问WiFi网络信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许程序读写手机状态和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- 代码混淆
3D 地图 V5.0.0之前:
-keep class com.amap.api.maps.**{*;}
-keep class com.autonavi.amap.mapcore.*{*;}
-keep class com.amap.api.trace.**{*;}
3D 地图 V5.0.0之后:
-keep class com.amap.api.maps.**{*;}
-keep class com.autonavi.**{*;}
-keep class com.amap.api.trace.**{*;}
定位
-keep class com.amap.api.location.**{*;}
-keep class com.amap.api.fence.**{*;}
-keep class com.autonavi.aps.amapapi.model.**{*;}
搜索
-keep class com.amap.api.services.**{*;}
2D地图
-keep class com.amap.api.maps2d.**{*;}
-keep class com.amap.api.mapcore2d.**{*;}
导航
-keep class com.amap.api.navi.**{*;}
-keep class com.autonavi.**{*;}
- 实现定位功能
添加地图控件
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
生命周期的管理
public class MainActivity extends Activity {
MapView mMapView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取地图控件引用
mMapView = (MapView) findViewById(R.id.map);
//在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
mMapView.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
mMapView.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
mMapView.onSaveInstanceState(outState);
}
}
显示地图
//定义了一个地图view
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);// 此方法须覆写,虚拟机需要在很多情况下保存地图绘制的当前状态。
//初始化地图控制器对象
AMap aMap;
if (aMap == null) {
aMap = mapView.getMap();
}
/**
* AMapV1地图中简单介绍显示定位小蓝点
*/
public class LocationSourceActivity extends Activity implements LocationSource,
AMapLocationListener {
private AMap aMap;
private MapView mapView;
private OnLocationChangedListener mListener;
private AMapLocationClient mlocationClient;
private AMapLocationClientOption mLocationOption;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationsource_activity);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);// 此方法必须重写
init();
}
/**
* 初始化AMap对象
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
setUpMap();
}
}
/**
* 设置一些amap的属性
*/
private void setUpMap() {
// 自定义系统定位小蓝点
MyLocationStyle myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationIcon(BitmapDescriptorFactory
.fromResource(R.drawable.location_marker));// 设置小蓝点的图标
myLocationStyle.strokeColor(Color.BLACK);// 设置圆形的边框颜色
myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));// 设置圆形的填充颜色
// myLocationStyle.anchor(int,int)//设置小蓝点的锚点
myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细
aMap.setMyLocationStyle(myLocationStyle);
aMap.setLocationSource(this);// 设置定位监听
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
// aMap.setMyLocationType()
}
/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
deactivate();
}
/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必须重写
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
/**
* 定位成功后回调函数
*/
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (mListener != null && amapLocation != null) {
if (amapLocation != null
&& amapLocation.getErrorCode() == 0) {
mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
} else {
String errText = "定位失败," + amapLocation.getErrorCode()+ ": " + amapLocation.getErrorInfo();
Log.e("AmapErr",errText);
}
}
}
/**
* 激活定位
*/
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(this);
mLocationOption = new AMapLocationClientOption();
//设置定位监听
mlocationClient.setLocationListener(this);
//设置为高精度定位模式
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
//设置定位参数
mlocationClient.setLocationOption(mLocationOption);
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
mlocationClient.startLocation();
}
}
/**
* 停止定位
*/
@Override
public void deactivate() {
mListener = null;
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient = null;
}
}
- 实现POI搜索
package com.amap.map2d.demo.poisearch;
import java.util.ArrayList;
import java.util.List;
import android.app.ProgressDialog;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMap.InfoWindowAdapter;
import com.amap.api.maps2d.AMap.OnMarkerClickListener;
import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.SupportMapFragment;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.NaviPara;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.SuggestionCity;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.Inputtips.InputtipsListener;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener;
import com.amap.map2d.demo.R;
import com.amap.map2d.demo.util.AMapUtil;
import com.amap.map2d.demo.util.ToastUtil;
import overlay.PoiOverlay;
/**
* AMapV1地图中简单介绍poisearch搜索
*/
public class PoiKeywordSearchActivity extends FragmentActivity implements
OnMarkerClickListener, InfoWindowAdapter, TextWatcher,
OnPoiSearchListener, OnClickListener, InputtipsListener {
private AMap aMap;
private AutoCompleteTextView searchText;// 输入搜索关键字
private String keyWord = "";// 要输入的poi搜索关键字
private ProgressDialog progDialog = null;// 搜索时进度条
private EditText editCity;// 要输入的城市名字或者城市区号
private PoiResult poiResult; // poi返回的结果
private int currentPage = 0;// 当前页面,从0开始计数
private PoiSearch.Query query;// Poi查询条件类
private PoiSearch poiSearch;// POI搜索
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poikeywordsearch_activity);
init();
}
/**
* 初始化AMap对象
*/
private void init() {
if (aMap == null) {
aMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
setUpMap();
}
}
/**
* 设置页面监听
*/
private void setUpMap() {
Button searButton = (Button) findViewById(R.id.searchButton);
searButton.setOnClickListener(this);
Button nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(this);
searchText = (AutoCompleteTextView) findViewById(R.id.keyWord);
searchText.addTextChangedListener(this);// 添加文本输入框监听事件
editCity = (EditText) findViewById(R.id.city);
aMap.setOnMarkerClickListener(this);// 添加点击marker监听事件
aMap.setInfoWindowAdapter(this);// 添加显示infowindow监听事件
}
/**
* 点击搜索按钮
*/
public void searchButton() {
keyWord = AMapUtil.checkEditText(searchText);
if ("".equals(keyWord)) {
ToastUtil.show(PoiKeywordSearchActivity.this, "请输入搜索关键字");
return;
} else {
doSearchQuery();
}
}
/**
* 点击下一页按钮
*/
public void nextButton() {
if (query != null && poiSearch != null && poiResult != null) {
if (poiResult.getPageCount() - 1 > currentPage) {
currentPage++;
query.setPageNum(currentPage);// 设置查后一页
poiSearch.searchPOIAsyn();
} else {
ToastUtil.show(PoiKeywordSearchActivity.this,
R.string.no_result);
}
}
}
/**
* 显示进度框
*/
private void showProgressDialog() {
if (progDialog == null)
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setIndeterminate(false);
progDialog.setCancelable(false);
progDialog.setMessage("正在搜索:\n" + keyWord);
progDialog.show();
}
/**
* 隐藏进度框
*/
private void dissmissProgressDialog() {
if (progDialog != null) {
progDialog.dismiss();
}
}
/**
* 开始进行poi搜索
*/
protected void doSearchQuery() {
showProgressDialog();// 显示进度框
currentPage = 0;
query = new PoiSearch.Query(keyWord, "", editCity.getText().toString());// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
query.setPageSize(10);// 设置每页最多返回多少条poiitem
query.setPageNum(currentPage);// 设置查第一页
query.setCityLimit(true);
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();
}
@Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
return false;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
@Override
public View getInfoWindow(final Marker marker) {
View view = getLayoutInflater().inflate(R.layout.poikeywordsearch_uri, null);
TextView title = (TextView) view.findViewById(R.id.title);
title.setText(marker.getTitle());
TextView snippet = (TextView) view.findViewById(R.id.snippet);
snippet.setText(marker.getSnippet());
ImageButton button = (ImageButton) view
.findViewById(R.id.start_amap_app);
// 调起高德地图app
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startAMapNavi(marker);
}
});
return view;
}
/**
* 调起高德地图导航功能,如果没安装高德地图,会进入异常,可以在异常中处理,调起高德地图app的下载页面
*/
public void startAMapNavi(Marker marker) {
// 构造导航参数
NaviPara naviPara = new NaviPara();
// 设置终点位置
naviPara.setTargetPoint(marker.getPosition());
// 设置导航策略,这里是避免拥堵
naviPara.setNaviStyle(AMapUtils.DRIVING_AVOID_CONGESTION);
// 调起高德地图导航
try {
AMapUtils.openAMapNavi(naviPara, getApplicationContext());
} catch (com.amap.api.maps2d.AMapException e) {
// 如果没安装会进入异常,调起下载页面
AMapUtils.getLatestAMapApp(getApplicationContext());
}
}
/**
* 判断高德地图app是否已经安装
*/
public boolean getAppIn() {
PackageInfo packageInfo = null;
try {
packageInfo = this.getPackageManager().getPackageInfo(
"com.autonavi.minimap", 0);
} catch (NameNotFoundException e) {
packageInfo = null;
e.printStackTrace();
}
// 本手机没有安装高德地图app
if (packageInfo != null) {
return true;
}
// 本手机成功安装有高德地图app
else {
return false;
}
}
/**
* 获取当前app的应用名字
*/
public String getApplicationName() {
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
try {
packageManager = getApplicationContext().getPackageManager();
applicationInfo = packageManager.getApplicationInfo(
getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
applicationInfo = null;
}
String applicationName = (String) packageManager
.getApplicationLabel(applicationInfo);
return applicationName;
}
/**
* poi没有搜索到数据,返回一些推荐城市的信息
*/
private void showSuggestCity(List<SuggestionCity> cities) {
String infomation = "推荐城市\n";
for (int i = 0; i < cities.size(); i++) {
infomation += "城市名称:" + cities.get(i).getCityName() + "城市区号:"
+ cities.get(i).getCityCode() + "城市编码:"
+ cities.get(i).getAdCode() + "\n";
}
ToastUtil.show(PoiKeywordSearchActivity.this, infomation);
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String newText = s.toString().trim();
if (!AMapUtil.IsEmptyOrNullString(newText)) {
InputtipsQuery inputquery = new InputtipsQuery(newText, editCity.getText().toString());
Inputtips inputTips = new Inputtips(PoiKeywordSearchActivity.this, inputquery);
inputTips.setInputtipsListener(this);
inputTips.requestInputtipsAsyn();
}
}
/**
* POI信息查询回调方法
*/
@Override
public void onPoiSearched(PoiResult result, int rCode) {
dissmissProgressDialog();// 隐藏对话框
if (rCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getQuery() != null) {// 搜索poi的结果
if (result.getQuery().equals(query)) {// 是否是同一条
poiResult = result;
// 取得搜索到的poiitems有多少页
List<PoiItem> poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始
List<SuggestionCity> suggestionCities = poiResult
.getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
if (poiItems != null && poiItems.size() > 0) {
aMap.clear();// 清理之前的图标
PoiOverlay poiOverlay = new PoiOverlay(aMap, poiItems);
poiOverlay.removeFromMap();
poiOverlay.addToMap();
poiOverlay.zoomToSpan();
} else if (suggestionCities != null
&& suggestionCities.size() > 0) {
showSuggestCity(suggestionCities);
} else {
ToastUtil.show(PoiKeywordSearchActivity.this,
R.string.no_result);
}
}
} else {
ToastUtil.show(PoiKeywordSearchActivity.this,
R.string.no_result);
}
} else {
ToastUtil.showerror(PoiKeywordSearchActivity.this, rCode);
}
}
@Override
public void onPoiItemSearched(PoiItem item, int rCode) {
// TODO Auto-generated method stub
}
/**
* Button点击事件回调方法
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
/**
* 点击搜索按钮
*/
case R.id.searchButton:
searchButton();
break;
/**
* 点击下一页按钮
*/
case R.id.nextButton:
nextButton();
break;
default:
break;
}
}
@Override
public void onGetInputtips(List<Tip> tipList, int rCode) {
if (rCode == AMapException.CODE_AMAP_SUCCESS) {// 正确返回
List<String> listString = new ArrayList<String>();
for (int i = 0; i < tipList.size(); i++) {
listString.add(tipList.get(i).getName());
}
ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(
getApplicationContext(),
R.layout.route_inputs, listString);
searchText.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
} else {
ToastUtil.showerror(PoiKeywordSearchActivity.this, rCode);
}
}
}