高德地图配置

1、打开高德开放平台
2、创建应用
3、添加新key:XXXXXXXXXXXXXXXXXXXXXXXXXXX
sha1值获取:找到系统默认的debug.keystore(C:\Users\Administrator\.android\debug.keystore)
在cmd窗口或android studio 中的Terminal中执行命令:keytool -v -list -keystore keystore地址
4、添加jar和so库(不要添加v4包)
so库添加方式:
第一种:使用默认配置,不需要修改build.gradle。在 main 目录下创建文件夹 jniLibs (如果有就不需要创建了),将下载文件的 armeabi 文件夹复制到这个目录下,如果已经有这个目录,将下载的 so 库复制到这个目录即可。
第二种:使用自定义配置,将下载文件的 armeabi 文件夹复制到 libs 目录,如果有这个目录,请将下载的 so 库复制到这个目录,然后打开build.gradle,找到 sourceSets 标签:
sourceSets{
main{
jniLibs.srcDirs = ['libs']
}
}
5、配置
1、配置AndroidManifest.xml
1、声明Service组件
<service android:name="com.amap.api.location.APSService"></service>
2、声明权限
<!--用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!--用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!--用于获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!--用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!--用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!--用于访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!--用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!--用于写入缓存数据到扩展存储卡-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!--用于申请调用A-GPS模块-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<!--用于申请获取蓝牙信息进行室内定位-->
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
3、设置高德Key
<meta-data android:name="com.amap.api.v2.apikey" android:value="key">//开发者申请的key       
            
</meta-data>
6、定位
1、声明成员变量
//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;
2、初始化定位
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
3、设置定位回调监听
mLocationClient.setLocationListener(new AMapLocationListener(){
@Override
            public void onLocationChanged(AMapLocation aMapLocation) {
//获取定位结果
}
});
4、定位结果处理
if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //可在其中解析amapLocation获取相应内容。
                int locationType = aMapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
                double latitude = aMapLocation.getLatitude();//获取纬度
                double longitude = aMapLocation.getLongitude();//获取经度
                float accuracy = aMapLocation.getAccuracy();//获取精度信息
                String address = aMapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址。
                aMapLocation.getCountry();//国家信息
                aMapLocation.getProvince();//省信息
                aMapLocation.getCity();//城市信息
                aMapLocation.getDistrict();//城区信息
                aMapLocation.getStreet();//街道信息
                aMapLocation.getStreetNum();//街道门牌号信息
                aMapLocation.getCityCode();//城市编码
                aMapLocation.getAdCode();//地区编码
                aMapLocation.getAoiName();//获取当前定位点的AOI信息
                aMapLocation.getBuildingId();//获取当前室内定位的建筑物Id
                aMapLocation.getFloor();//获取当前室内定位的楼层
                //获取定位时间
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(aMapLocation.getTime());
                df.format(date);
                Log.e("TAG", "纬度:" + latitude + "   经度:" + longitude + "  地址:" + address);
            } else {
                //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                    + aMapLocation.getErrorCode() + ", errInfo:"
                    + aMapLocation.getErrorInfo());
            }
}

7、地图
1、在xml中声明MapView控件
2、在activity中初始化MapView
3、在声明周期中调用MapView的声明周期:onCreate\onResume\onPause\onDestroy\onSaveInstanceState

8、蓝点
1、创建MyLocationStyle对象: style = new MyLocationStyle();
2、设置定位间隔时间:style.interval(2000);
3、设置圆形区域:
1、圆形区域填充色:style.radiusFillColor(Color.rgb(0, 255, 255));
2、圆形区域(以定位位置为圆心,定位半径的圆形区域)的边框颜色:style.strokeColor(Color.rgb(0, 100, 255));
3、圆形区域(以定位位置为圆心,定位半径的圆形区域)的边框宽度:style.strokeWidth(2F);
4、设置定位模式和蓝点模式:(选择其一)
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);//只定位一次。
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE) ;//定位一次,且将视角移动到地图中心点。
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW) ;//连续定位、且将视角移动到地图中心点,定位蓝点跟随设备移动。(1秒1次定位)
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);//连续定位、且将视角移动到地图中心点,地图依照设备方向旋转,定位点会跟随设备移动。(1秒1次定位)
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)默认执行此种模式。
//以下四种模式从5.1.0版本开始提供
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)默认执行此种模式。
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);//连续定位、蓝点不会移动到地图中心点,定位点依照设备方向旋转,并且蓝点会跟随设备移动。
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER);//连续定位、蓝点不会移动到地图中心点,并且蓝点会跟随设备移动。
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE_NO_CENTER);//连续定位、蓝点不会移动到地图中心点,地图依照设备方向旋转,并且蓝点会跟随设备移动。
9、POI
1、创建PoiSearch.Query对象
query = new PoiSearch.Query(keyWord, "", cityCode);
//keyWord表示搜索字符串,
//第二个参数表示POI搜索类型,二者选填其一,选用POI搜索类型时建议填写类型代码,码表可以参考下方(而非文字)
//cityCode表示POI搜索区域,可以是城市编码也可以是城市名称,也可以传空字符串,空字符串代表全国在全国范围内进行搜索
query.setPageSize(10);// 设置每页最多返回多少条poiitem
query.setPageNum(currentPage);//设置查询页码
2、构造 PoiSearch 对象,并设置监听。
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
3、调用 PoiSearch 的 searchPOIAsyn() 方法发送请求。
poiSearch.searchPOIAsyn();
4、通过回调接口 onPoiSearched 解析返回的结果,将查询到的 POI 以绘制点的方式显示在地图上。
10、覆盖物(绘制点)
1、创建绘制点:
LatLng latLng = new LatLng(39.906901,116.397972);
final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title("北京").snippet("DefaultMarker"));
2、设置Marker点击事件
// 定义 Marker 点击事件监听
AMap.OnMarkerClickListener markerClickListener = new AMap.OnMarkerClickListener() {
// marker 对象被点击时回调的接口
// 返回 true 则表示接口已响应事件,否则返回false
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
};
// 绑定 Marker 被点击事件
mAMap.setOnMarkerClickListener(markerClickListener);
11、路径规划
1、路径规划实现
1、公共交通
aMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
BusResultListAdapter mBusResultListAdapter = new BusResultListAdapter(mContext, result);
mBusResultList.setAdapter(mBusResultListAdapter);
} else if (result != null && result.getPaths() == null) {
ToastUtil.show(mContext, R.string.no_result);
}
} else {
ToastUtil.show(mContext, R.string.no_result);
}
} else {
ToastUtil.showerror(this.getApplicationContext(), errorCode);
}
2、自驾
aMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
mDriveRouteResult = result;
final DrivePath drivePath = mDriveRouteResult.getPaths()
.get(0);
DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
mContext, aMap, drivePath,
mDriveRouteResult.getStartPos(),
mDriveRouteResult.getTargetPos(), null);
drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况,默认true
drivingRouteOverlay.removeFromMap();
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan();
} else if (result != null && result.getPaths() == null) {
ToastUtil.show(mContext, R.string.no_result);
}


} else {
ToastUtil.show(mContext, R.string.no_result);
}
} else {
ToastUtil.showerror(this.getApplicationContext(), errorCode);
}
3、走路
aMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
mWalkRouteResult = result;
final WalkPath walkPath = mWalkRouteResult.getPaths()
.get(0);
WalkRouteOverlay walkRouteOverlay = new WalkRouteOverlay(
this, aMap, walkPath,
mWalkRouteResult.getStartPos(),
mWalkRouteResult.getTargetPos());
walkRouteOverlay.removeFromMap();
walkRouteOverlay.addToMap();
walkRouteOverlay.zoomToSpan();
} else if (result != null && result.getPaths() == null) {
ToastUtil.show(mContext, R.string.no_result);
}
} else {
ToastUtil.show(mContext, R.string.no_result);
}
} else {
ToastUtil.showerror(this.getApplicationContext(), errorCode);
}
4、骑车
aMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
mRideRouteResult = result;
final RidePath ridePath = mRideRouteResult.getPaths()
.get(0);
RideRouteOverlay rideRouteOverlay = new RideRouteOverlay(
this, aMap, ridePath,
mRideRouteResult.getStartPos(),
mRideRouteResult.getTargetPos());
rideRouteOverlay.removeFromMap();
rideRouteOverlay.addToMap();
rideRouteOverlay.zoomToSpan();
} else if (result != null && result.getPaths() == null) {
ToastUtil.show(mContext, R.string.no_result);
}
} else {
ToastUtil.show(mContext, R.string.no_result);
}
} else {
ToastUtil.showerror(this.getApplicationContext(), errorCode);
}
2、路径规划之监听中接受数据
需要复制的文件:(需修改包引用)
实现接口  :RouteSearch.OnRouteSearchListener
1、overlay包下所有类
2、com\amap\map3d\demo\util包下:
AMapUtil.java
ChString.java
ToastUtil.java
3、res\drawable-hdpi下:
amap_bus.png
amap_car.png
amap_end.png
amap_man.png
amap_ride.png
amap_start.png
amap_through.png

dir1.png-dir16.png
4、res\values下strings.xml中:<string name="no_result">对不起,没有搜索到相关数据!</string>









路径规划代码:aMap.clear();// 清理地图上的所有覆盖物
监听中
         aMap.clear();// 清理地图上的所有覆盖物


        if (i == AMapException.CODE_AMAP_SUCCESS) {
            if (driveRouteResult != null && driveRouteResult.getPaths() != null) {
                if (driveRouteResult.getPaths().size() > 0) {
//                    mDriveRouteResult = result;
                    final DrivePath drivePath = driveRouteResult.getPaths()
                            .get(0);
                    DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
                            MapActivity.this, aMap, drivePath,
                            driveRouteResult.getStartPos(),
                            driveRouteResult.getTargetPos(), null);
                    drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
                    drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况,默认true
                    drivingRouteOverlay.removeFromMap();
                    drivingRouteOverlay.addToMap();
                    drivingRouteOverlay.zoomToSpan();


                }
            }
        }


 Location myLocation = aMap.getMyLocation();
        double latitude = myLocation.getLatitude();
        double longitude = myLocation.getLongitude();
        LatLonPoint oldPoint = new LatLonPoint(latitude, longitude);


        LatLng position = marker.getPosition();
        LatLonPoint point = new LatLonPoint(position.latitude, position.longitude);


        final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(oldPoint, point);
RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DRIVING_MULTI_STRATEGY_FASTEST_SAVE_MONEY_SHORTEST, null, null, "");
                routeSearch.calculateDriveRouteAsyn(query);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值