Osmdroid load third-party map googlemap, bingmap etc.

1.osmdroid way to load third-party map

The first: FIG source by way of expansion

The second: (such as google maps api packaging) by packaging, called with a unified interface.

2.osmdroid-third-party source Introduction

osmdroid provides third-party extensions to load the map, mainly by studying its source code, learn how to use the interface provided osmdroid achieve load third-party map, but more important is to learn thinking of them.

Osmdroid implemented in the source code module is osmdroid-third-party, take a look at the structure:

          

api defines a number of interfaces and their implementation classes

google is defined in the overlay and wrapper packaging by the second way

bing bing loaded in the map by the first way

3.google packaging design

Look at a few sample categories:

GeoPoint class

public class GeoPoint implements IGeoPoint {

   private final com.google.android.maps.GeoPoint mGeoPoint;

   public GeoPoint(final com.google.android.maps.GeoPoint pGeoPoint) {
      mGeoPoint = pGeoPoint;
   }

MapController

public class MapController implements IMapController {

   private final com.google.android.maps.MapController mController;

   public MapController(final com.google.android.maps.MapController pController) {
      mController = pController;
   }
MapView
public class MapView implements IMapView {

   private final com.google.android.maps.MapView mMapView;

   public MapView(final com.google.android.maps.MapView pMapView) {
      mMapView = pMapView;
   }

MapWrapper implement the interface and use IMap held googlemap achieve corresponding map operation

class MapWrapper implements IMap {

   private GoogleMap mGoogleMap;
   private HashMap<Integer, com.google.android.gms.maps.model.Polyline> mPolylines;
   private static final Random random = new Random();

   MapWrapper(final GoogleMap aGoogleMap) {
      mGoogleMap = aGoogleMap;

   }

   @Override
   public float getZoomLevel() {
      final CameraPosition cameraPosition = mGoogleMap.getCameraPosition();
      return cameraPosition.zoom;
   }

We can clearly see that these kind of features are a custom class, then both held google corresponding class, concrete implementation is achieved through the google api, but it is time to call their own calls defined interfaces, such as when called, simply, is calling rely abstract, does not depend on the specific implementation (this sentence really is the truth).


IMap mMap = MapFactory.getMap(mMapViewV2);

final Position position = new Position(52.370816, 9.735936); // Hannover
position.setZoomLevel(14);
mMap.setPosition(position);
mMap.setMyLocationEnabled(false);
mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
    @Override
    public void onCameraChange(final IPosition position) {
        Log.d(Constants.LOGTAG, "onCameraChange");
    }
});
MapFactory.getMap source
public static IMap getMap(final com.google.android.gms.maps.MapView aMapView) {
   final GoogleMap map = aMapView.getMap();
   MapsInitializer.initialize(aMapView.getContext());
   return map != null ? new MapWrapper(map) : null;
}
Api IMap wherein the interface is defined in the package, wherein the interface defines a number of maps relating to operations, such as zoom, the center point is provided, the boundary, monitor, etc., the source follows iMap,
public interface IMap {

  
   float getZoomLevel();

   
   void setZoom(float zoomLevel);

   IGeoPoint getCenter();

   
   void setCenter(double latitude, double longitude);

   
   float getBearing();

  
   void setBearing(float bearing);

  
   void setPosition(IPosition position);

   
   boolean zoomIn();

  
   boolean zoomOut();

   void setMyLocationEnabled(boolean enabled);

   
   boolean isMyLocationEnabled();

  
   IProjection getProjection();

   
   void addMarker(Marker marker);

  
   int addPolyline(Polyline polyline);

   
   void addPointsToPolyline(int id, IGeoPoint... points);

  
   void clearPolyline(int id);

   
   void clear();

   
   void setOnCameraChangeListener(OnCameraChangeListener listener);
}

问题来了,这么费劲的要自定义这么多的类到底有什么好处!!!不能只出活不讨好吧。其实这应用了一种设计模式--装饰设计,没了解过的可以看一下我的另一篇博客http://blog.csdn.net/qq_30124547/article/details/53184754很简单的一个例子,但是基本上可以理解了。

现在,突然google地图不能用了,我们需要换成bing地图,也就是微软的地图,该怎么办,我们只需要写一个BingWrapper继承IMap并实现对应的地图操作即可,实际调用的地方改动的地方很小,以前总是不注重设计模式,但是自从知道它的好之后,就再也停不下来了,其实还有一种代理模式跟装饰设计很想——代理模式,虽然很像,但还是有区别的。


注意:要想用谷歌的api,必须用注册一个api-key的东西,需要包名+签名文件的sha1值生成,而且更换签名、包名、电脑、、、、key都将不能使用,需要重新生成,详细的还是看官方的中文指南。点我穿越

还有要注意一点的就是,在国内使用google地图需要翻墙才能显示。不然只会显示网格,没有地图数据。

4.拓展图源的方式加载bing地图

拓展图源的方式相对来说比包装的形式更方便快捷,但是同时也会有很多的问题

官方给的示例是使用bing地图的图源

public class BingMapTileSource extends QuadTreeTileSource implements IStyledTileSource<String>

实现方式是继承QuadTreeTileSource,实现IStyledTileSource<String>接口,实现对应的方法即可,其中比较重要的就是瓦片源地址

// URL used to get imageryData. It is requested in order to get tiles url patterns
private static final String BASE_URL_PATTERN = "http://dev.virtualearth.net/REST/V1/Imagery/Metadata/%s?mapVersion=v1&output=json&key=%s";

具体便不再详细介绍,具体调用就很简单:

BingMapTileSource source = new BingMapTileSource(null);

mMapView.setTileSource(source);
然后所有逻辑都是osmdroid自身的逻辑相同。


Guess you like

Origin blog.csdn.net/qq_30124547/article/details/53307486