关于申请key等,请参考加载一张简单的高德地图
地图是由多个图层组成,每个图层会显示一部分的地理或交通信息。开发
者可以通过设置 AMap 类,灵活控制图层的显示状态。例如,用户所看到包括
街道、兴趣点、学校、公园等内容的地图展现就是一个图层。实时路况等的展
现也是通过图层来实现的。首先介绍一下比较常用的四种图层:
• 基本地图。
• 自定义图层(这里不做讲解)。
• 实时路况。
• 定位层。
3D地图SDK提供三种地图类型:
1.MAP_TYPE_NORMAL
标准地图。地图包含道路、建筑等。
2. MAP_TYPE_SATELLITE
卫星地图。
3. MAP_TYPE_NIGHT
夜景地图(仅3D地图)
AMap是地图的控制类 ,设置基础地图的类型只需要
AMap aMap=mapView.getMap();
aMap.setMapType(AMap.MAP_TYPE_NORMAL);
AMap aMap=mapView.getMap();
aMap.setTrafficEnabled(true);
这里涉及到定位需要从高德官网下载定位的相关jar包,这里使用
AMap_Location_V2.5.0_20160526.jar
这个有些繁琐,但都是固定的模板
// 设置激活/关闭定位的监听
aMap.setLocationSource(this);
//在地图上标注我的位置
aMap.getUiSettings().setMyLocationButtonEnabled(true);
// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
aMap.setMyLocationEnabled(true);
//设置当前位置跟随模式
//参数:
//type - style 总共有三种模式,定位,跟随和旋转(这里为跟随)
aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);
LocationSource 方法如下:
public interface LocationSource {
//激活定位
void activate(LocationSource.OnLocationChangedListener var1);
//取消定位
void deactivate();
public interface OnLocationChangedListener {
void onLocationChanged(Location var1);
}
}
激活定位,但定位逻辑是由——AMapLocationClient来进行的
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
this.mListener = onLocationChangedListener;
if (aMapLocationClient == null) {
aMapLocationClient = new AMapLocationClient(this);
aMapLocationClientOption = new AMapLocationClientOption();
//设置定位回调,处理位置改变
aMapLocationClient.setLocationListener(this);
//设置定位模式,推荐使用高精度定位
aMapLocationClientOption
.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置aMapLocationClient的定位Option
aMapLocationClient.setLocationOption(aMapLocationClientOption);
//开启定位
aMapLocationClient.startLocation();
}
在AMapLocationListener .onLocationChanged()中处理位置改变
public interface AMapLocationListener {
void onLocationChanged(AMapLocation var1);
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (mListener != null && aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
//这里高德地图进行了高度封装,直接就显示系统小蓝点
mListener.onLocationChanged(aMapLocation);
} else {
String errText = "定位失败," + aMapLocation.getErrorCode() + ": "
+ aMapLocation.getErrorInfo();
Log.e("AmapErr", errText);
}
}
}
注意:获取手机位置属于危险权限,需要进行申请,不熟悉的同学请参考
Android6.0权限申请模型
完整代码如下:
package com.hang.hellomapdemo;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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.LocationSource;
import com.amap.api.maps.MapView;
public class SimpleLocationActivity extends AppCompatActivity implements LocationSource, AMapLocationListener {
MapView mapView;
AMap aMap;
OnLocationChangedListener mListener;
AMapLocationClient aMapLocationClient;
AMapLocationClientOption aMapLocationClientOption;
final int REQUEST_PERMISSION_LOCATION = 10;
@TargetApi(23)
public void applyPermission() {
ActivityCompat.requestPermissions(SimpleLocationActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_PERMISSION_LOCATION);
}
@TargetApi(23)
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_PERMISSION_LOCATION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
aMap.setLocationSource(this);
//在地图上标注我的位置
aMap.getUiSettings().setMyLocationButtonEnabled(true);
// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
aMap.setMyLocationEnabled(true);
//设置定位类型 AMap.LOCATION_TYPE_MAP_FOLLOW:追踪定位
aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);
} else {
Toast.makeText(SimpleLocationActivity.this, "没有获取权限,无法完成定位~", Toast.LENGTH_LONG).show();
}
}
break;
default:
break;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapView = new MapView(this);
aMap = mapView.getMap();
setContentView(mapView);
mapView.onCreate(savedInstanceState);
applyPermission();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
this.mListener = onLocationChangedListener;
if (aMapLocationClient == null) {
aMapLocationClient = new AMapLocationClient(this);
aMapLocationClientOption = new AMapLocationClientOption();
aMapLocationClient.setLocationListener(this);
aMapLocationClientOption
.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
aMapLocationClient.setLocationOption(aMapLocationClientOption);
aMapLocationClient.startLocation();
}
}
@Override
public void deactivate() {
mListener = null;
if (aMapLocationClient != null) {
aMapLocationClient.stopLocation();
aMapLocationClient.onDestroy();
}
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (mListener != null && aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
} else {
String errText = "定位失败," + aMapLocation.getErrorCode() + ": "
+ aMapLocation.getErrorInfo();
Log.e("AmapErr", errText);
}
}
}
}