关于Osmdroid的一切
程序员文章站
2022-06-04 14:40:54
...
Osmdroid的离线地图:
MapView offlineMapView = (MapView)rootView.findViewById(R.id.map);
mapController = (MapController) offlineMapView.getController();
ResourceProxy resourceProxy = new DefaultResourceProxyImpl(myContext);
offlineMapView.setBuiltInZoomControls(true);
offlineMapView.setMultiTouchControls(true);
offlineMapView.setUseDataConnection(false);
offlineMapView.setMinZoomLevel(4);
offlineMapView.setMaxZoomLevel(30);offlineMapView.setTilesScaledToDpi(true);
double latitude = 52.270421;
double longitude = 10.533084;
GeoPoint startPoint = new GeoPoint(latitude, longitude);
Log.i(INFO, "latitude: " + latitude + " longitude:" + longitude);
mapController.setZoom(18);
mapController.setCenter(startPoint);
mapController.zoomIn();
mapController.zoomOut();
Osmdroid定位:
参考资料
核心代码:
private MyLocationNewOverlay myLocationOverlay;
private GeoUpdateListener myGeoUpdateListener;
private LocationManager myLocationManager;
定位相关的单独写在getLocation()中:
private void getLocation(MapController pMapController) {
myGeoUpdateListener = new GeoUpdateListener(pMapController);
myLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
/** * A class indicating the application criteria for selecting a location provider. Providers maybe ordered according to accuracy, power usage, ability to report altitude, speed, and bearing, and monetary cost. */
Criteria myCriteria = new Criteria();
myCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = myLocationManager.getBestProvider(myCriteria, true);
if (provider == null) {
Log.e(INFO, "ERROR: No location provider found!");
return;
} else {
if (ActivityCompat.checkSelfPermission(
this.getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION) !=PackageManager.PERMISSION_GRANTED && ActivityCompat .checkSelfPermission(
this.getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
return;
}
myLocationManager.requestLocationUpdates(provider, 500, 1, myGeoUpdateListener);
}
}
上面这段代码中需要说明的是方法.requestLocationUpdates(String provider, long minTime,float minDistance,LocationListener listener)
参数一 (String)provider: the name of the provider with which to register
参数二 (long)minTime: minimum time interval between location updates, in milliseconds
参数三 (float)minDistance : minimum distance between location updates, in meters
参数四 (LocationListener)listener: a LocationListener whose onLocationChanged(Location) method will be called for each location update
Osmdroid的Overlay有很多种,这里列举几种我试过的
1. ItemizedOverlay
ItemizedOverlay例子
核心代码:
Drawable drawable = ContextCompat.getDrawable(this.getActivity(), R.mipmap.ic_plant_point);
ResourceProxy resourceProxy = new DefaultResourceProxyImpl(this.getActivity().getApplicationContext());
MyOverlay myOverlay = new MyOverlay(drawable, resourceProxy);
offlineMapView.getOverlays().add(myOverlay);GeoPoint point1 = new GeoPoint(52.270421, 10.533084);
myOverlay.addItem("Kamelienblüten", "", point1);
GeoPoint point2 = new GeoPoint(52.2712111, 10.5321806);
myOverlay.addItem("Süntelbuche", "", point2);
MyOverlay.java:
package de.chuying.bota.util;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IMapView;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import java.util.ArrayList;
public class MyOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> overlayItemArrayList=new ArrayList<OverlayItem>();
public MyOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy) {
super(pDefaultMarker, pResourceProxy);
}
public void addItem(String title, String snippet,GeoPoint geoPoint){
OverlayItem myItem=new OverlayItem(title,snippet,geoPoint);
overlayItemArrayList.add(myItem);
populate(); //打印输出
}
@Override
protected OverlayItem createItem(int i) {
return overlayItemArrayList.get(i);
}
@Override
public int size() {
return overlayItemArrayList.size();
}
@Override
public boolean onSnapToItem(int i, int i1, Point point, IMapView iMapView) {
return false;
}
}
2. ItemizedOverlayWithFocus
与1区别是ItemizedOverlayWithFocus可获取焦点
转载于:https://www.jianshu.com/p/4e948afa5bb6