Android 高德地图集成
在集成之前都是需要在高德的开发平台上创建应用的,获取appkey和地图的SDK,这些步骤在之前的高德定位里都有,所以这里就不在重复了 高德定位
先上效果图
项目配置
第一种通过jar包配置
将下载的地图 SDK 的 jar包复制到工程
添加 so 库:
3D地图才需要添加so库,2D地图无需这一步骤。
导入so文件也有两种方式
1:在 main 目录下创建文件夹 jniLibs。将下载的SDK里的其他文件夹添加进去。
2:(1)下载文件的 armeabi 文件夹复制到 libs 目录
(2)在app的build里添加,在android里添加
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
第二种配置
app的build里添加,在android里defaultConfig里添加
ndk {
//设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so)
abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86","x86_64"
}
依赖
//3D地图so及jar
compile 'com.amap.api:3dmap:latest.integration'
//定位功能
compile 'com.amap.api:location:latest.integration'
//搜索功能
compile 'com.amap.api:search:latest.integration'
在清单文件里添加的
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="请输入您的用户Key"/>
权限
//地图包、搜索包需要的基础权限
<!--允许程序打开网络套接字-->
<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" />
这是activity_main.xml里的布局,里面的mapview就是展示地图的
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/send_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标准地图" />
<Button
android:layout_toRightOf="@id/send_one"
android:id="@+id/send_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="导航地图" />
<Button
android:layout_toRightOf="@id/send_two"
android:id="@+id/send_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="夜景地图" />
<Button
android:layout_toRightOf="@id/send_three"
android:id="@+id/send_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="卫星地图" />
<Button
android:layout_below="@id/send_one"
android:id="@+id/send_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="路况图层" />
</RelativeLayout>
这mainactivity的代码
public class MainActivity extends AppCompatActivity implements LocationSource, AMapLocationListener, View.OnClickListener {
private MapView map;
private AMap aMap;
private MyLocationStyle myLocationStyle;
OnLocationChangedListener mListener;
AMapLocationClient mlocationClient;
AMapLocationClientOption mLocationOption;
private Button send_one;
private Button send_two;
private Button send_three;
private Button send_four;
private Button send_five;
private UiSettings mUiSettings;//定义一个UiSettings对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
map.onCreate(savedInstanceState);
//初始化地图控制器对象
if (aMap == null) {
aMap = map.getMap();
}
//小蓝标显示
// 设置定位监听
aMap.setLocationSource(MainActivity.this);
// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
aMap.setMyLocationEnabled(true);
// 设置定位的类型为定位模式,有定位、跟随或地图根据面向方向旋转几种
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
mUiSettings = aMap.getUiSettings();//实例化UiSettings类对象
mUiSettings.setCompassEnabled(true);//指南针显示
aMap.setLocationSource(this);//通过aMap对象设置定位数据源的监听
mUiSettings.setMyLocationButtonEnabled(true); //显示默认的定位按钮
aMap.setMyLocationEnabled(true);// 可触发定位并显示当前位置
mUiSettings.setScaleControlsEnabled(true);//控制比例尺控件是否显示
LatLng latLng = new LatLng(39.906901,116.397972);
final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title("北京").snippet("DefaultMarker"));
}
private void initView() {
map = (MapView) findViewById(R.id.map);
send_one = (Button) findViewById(R.id.send_one);
send_one.setOnClickListener(this);
send_two = (Button) findViewById(R.id.send_two);
send_two.setOnClickListener(this);
send_three = (Button) findViewById(R.id.send_three);
send_three.setOnClickListener(this);
send_four = (Button) findViewById(R.id.send_four);
send_four.setOnClickListener(this);
send_five = (Button) findViewById(R.id.send_five);
send_five.setOnClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
map.onDestroy();
if (null != mlocationClient) {
mlocationClient.onDestroy();
}
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
map.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
map.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
map.onSaveInstanceState(outState);
}
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
mListener = onLocationChangedListener;
if (mlocationClient == null) {
//初始化定位
mlocationClient = new AMapLocationClient(this);
//初始化定位参数
mLocationOption = new AMapLocationClientOption();
//设置定位回调监听
mlocationClient.setLocationListener(this);
//设置为高精度定位模式
mLocationOption.setLocationMode(AMapLocationClientOption.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;
}
@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 onClick(View v) {
switch (v.getId()) {
case R.id.send_one:
aMap.setMapType(AMap.MAP_TYPE_NORMAL);//标准地图,aMap是地图控制器对象
break;
case R.id.send_two:
aMap.setMapType(AMap.MAP_TYPE_NAVI);//导航地图,aMap是地图控制器对象
break;
case R.id.send_three:
aMap.setMapType(AMap.MAP_TYPE_NIGHT);//夜景地图,aMap是地图控制器对象
break;
case R.id.send_four:
aMap.setMapType(AMap.MAP_TYPE_SATELLITE);// 设置卫星地图模式,aMap是地图控制器对象
break;
case R.id.send_five:
aMap.setTrafficEnabled(true);//显示实时路况图层,aMap是地图控制器对象。
break;
}
}
}