愿你出走半生,归来仍是少年!
在移动端的Gis应用中,定位是必不可少的实用性功能。通过Overlay的派生类MyLocationNewOverlay可实现Gps以及网络定位数据的实时展示。
1.构造
MyLocationNewOverlay(MapView mapView) | |
---|---|
MyLocationNewOverlay(IMyLocationProvider myLocationProvider, MapView mapView) |
通过传入MapView即可创建实例图层,然后可以直接添加到地图中。
2.二次封装的定位图层
package spec.map.osm.overlayers.functions;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
import spec.map.osm.overlayers.IBasicLayer;
/**
* 定位图层
*/
public class MyLocationOverlayer extends MyLocationNewOverlay implements IBasicLayer {
protected String name;
protected String displayeName;
/**
* 定位图层
* @param mapView
*/
public MyLocationOverlayer(MapView mapView) {
super(mapView);
this.name="LocationLayer";
this.displayeName="定位图层";
GpsMyLocationProvider provider= (GpsMyLocationProvider) mMyLocationProvider;
provider.setLocationUpdateMinTime(5000);
provider.setLocationUpdateMinDistance(4);
enableMyLocation(provider);
}
/**
* 设置图层元名称
* @param lyName
*/
@Override
public void setName(String lyName) {
this.name = lyName;
}
/**
* 获取图层元名称
* @return
*/
@Override
public String getName() {
return name;
}
/**
* 设置图层显示名称
* @param lyDisplayName
*/
@Override
public void setDisplayName(String lyDisplayName) {
this.displayeName = lyDisplayName;
}
/**
* 图层图层显示名称
* @return
*/
@Override
public String getDisplayName() {
return displayeName;
}
@Override
@Deprecated
public void setTransparency(int trans) {
}
@Override
@Deprecated
public int getTransparency() {
return 0;
}
}
3.图层使用
/**
* 定位图层
*/
private MyLocationOverlayer locationOverlayer;
locationOverlayer = new MyLocationOverlayer(map);
controlGroupLayer.add(locationOverlayer);
/**
* 获取定位图层
*
* @return
*/
public MyLocationOverlayer getLocationOverlayer() {
return locationOverlayer;
}
/**
* 显示定位图层
*/
public void showLocationLayer() {
locationOverlayer.setEnabled(false);
}
/**
* 隐藏定位图层
*/
public void hideLocationLayer() {
locationOverlayer.setEnabled(false);
}
/**
* 获取最后一次定位位置
*
* @return 可能为null
*/
public GeoPoint getLastGpsLocation() {
if (!getOverlayManager().getLocationOverlayer().isEnabled()) {
return null;
}
if (!getOverlayManager().getLocationOverlayer().isMyLocationEnabled()) {
return null;
}
var pt = getOverlayManager().getLocationOverlayer().getMyLocation();
return pt;
}