刚做完地图这块,顺便记个笔记,要不时间长了就忘了(ˇˍˇ)
osmdroid 下载地址:https://github.com/osmdroid/osmdroid
OSMDroid是一款基于Android的模块化地图引擎,如果你觉得Android内置的MapView 类不能满足项目的要求,那么可以试试OSMDroid。它是完全模块化的,可以让你的Android应用程序更加灵活可控。OSMDroid支持在线和离线地图以及覆盖地图,支持标注图标、位置跟踪和绘制形状等功能,因此你可以完全定制地图的展示方式和地图数据的存取。
osmdroid的定位不像高德、百度的一样,自带的没有定位接口,需要使用Android 系统的LocationManager实现定位功能,代码如下:
首先获取
LocationManager locationManager = (LocationManager)mapView.getContext().getSystemService(Context.LOCATION_SERVICE);
使用GPS定位:
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mHelper.locationListener);
网络定位
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, mHelper.locationListener);
创建locationListener监听
LocationListener locationListener = new LocationListener() {
//定位成功调用
@Override
public void onLocationChanged(Location location) {
}
//定位状态改变时调用
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
//定位可用时调用
@Override
public void onProviderEnabled(String provider){ }
//定位关闭时调用
@Override
public void onProviderDisabled(String provider{
}
}
需要的权限
需要注意的是在6.0以上动态权限申请:
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE
设置图层缩放
mScaleBarOverlay = new ScaleBarOverlay(mapView);
IMapController mController = mapView.getController();
mController.setZoom(18);//地图显示级别
//设置显示指南针
compassOverlay = new CompassOverlay(mapView.getContext(), new InternalCompassOrientationProvider(mapView.getContext()), mapView);
//显示图层
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapView.setFlingEnabled(true);
mapView.setTilesScaledToDpi(true);
设置显示定位标记
Bitmap bMap = BitmapFactory.decodeResource(mapView.getContext().getResources(), R.mipmap.gps_point);
myLocationOverlay = new DirectedLocationOverlay
(mapView.getContext());
myLocationOverlay.setDirectionArrow(bMap);
myLocationOverlay.setEnabled(true);
定位的还有一个api SimpleLocationOverlay,这个已经过时了,添加定位图片后与实际经纬度不符合,大家就不要用这个了(ˇˍˇ)
地图移动到某个点
mapView.getController().animateTo(geoPoint);
添加到图层中
mapView.getOverlays().add(mScaleBarOverlay);
mapView.getOverlays().add(myLocationOverlay);
绘制路径
Polyline polyline = new Polyline();
polyline.setColor(color);
polyline.setWidth(10.0f);
polyline.setGeodesic(true);
polyline.setVisible(true);
polyline.setEnabled(true);
polyline.setPoints(geoPointList);
mapView.getOverlays().add(polyline);
希望能帮助到你!!!!!