android osmdroid 加载离线地图map格式以及地图网格绘制

越来越深入了解osmdroid开源地图,越发现强大。继上次记载离线地图文章之后android osmdroid 加载常用离线地图格式(开源的在线地图)。再讲支持map地图格式的数据。还有怎么绘制地图的网格,绘制网络分为两种,一种是直接在地图上绘制,监听缩放等级。还有一种是利用Fragment加一层进行绘制。

osmdroid 仓库地址:http://jcenter.bintray.com/org/osmdroid/osmdroid-android/  这里面有示例apk,还有源码以及文档。

github官网:https://github.com/osmdroid/osmdroid

mapsforge相关库的仓库地址:http://jcenter.bintray.com/org/mapsforge/

map地图格式文件下载:http://ftp-stud.hs-esslingen.de/pub/Mirrors/download.mapsforge.org/maps/

1,先看实现的界面

网格实现



map地图格式



2,地图网格相关代码

直接加载版本,增加缩放监听即可

package com.osmdroid.sample;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent;
import org.osmdroid.events.ZoomEvent;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.FolderOverlay;
import org.osmdroid.views.overlay.gridlines.LatLonGridlineOverlay;

public class DirectGridActivity extends AppCompatActivity implements View.OnClickListener {
    FolderOverlay activeLatLonGrid;
    private MapView mMapView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        initView();
    }

    private void initView() {
        mMapView = (MapView)findViewById(R.id.mymapview);

        LatLonGridlineOverlay.setDefaults();
        mMapView.setMaxZoomLevel(20);
        mMapView.setMinZoomLevel(6);
        mMapView.getController().setZoom(12);
        mMapView.setTilesScaledToDpi(true);
        mMapView.getController().setCenter(new GeoPoint(23.12645, 113.365575));
        mMapView.setUseDataConnection(true);
        mMapView.setMultiTouchControls(true);// 触控放大缩小
        mMapView.getOverlayManager().getTilesOverlay().setEnabled(true);
        mMapView.setMapListener(mapListener);
        LatLonGridlineOverlay.fontSizeDp=16;
        LatLonGridlineOverlay.fontColor= Color.argb(255,0,255,0);
        LatLonGridlineOverlay.backgroundColor=Color.BLACK;
        LatLonGridlineOverlay.lineColor=LatLonGridlineOverlay.fontColor;
        updateGridlines();
    }

    MapListener mapListener = new MapListener() {
        @Override
        public boolean onScroll(ScrollEvent scrollEvent) {
            updateGridlines();
            return false;
        }

        @Override
        public boolean onZoom(ZoomEvent zoomEvent) {
            updateGridlines();
            return false;
        }
    };

    protected void updateGridlines(){

        if (mMapView==null)
            return; //happens during unit tests with rapid recycling of the fragment
        if (activeLatLonGrid != null) {
            mMapView.getOverlayManager().remove(activeLatLonGrid);
            activeLatLonGrid.onDetach(mMapView);
        }
        LatLonGridlineOverlay.backgroundColor= Color.BLACK;
        LatLonGridlineOverlay.fontColor= Color.BLUE;
        LatLonGridlineOverlay.lineColor= Color.BLUE;
        activeLatLonGrid = LatLonGridlineOverlay.getLatLonGrid(this, mMapView);
        mMapView.getOverlays().add(activeLatLonGrid);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:


                break;
        }
    }
}

使用Fragment绘制格网

package com.osmdroid.sample;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import com.osmdroid.sample.util.SampleGridlines;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;

public class GridFragmentActivity extends AppCompatActivity  {

    private SampleGridlines mSampleGridlines = null;
    private MapView mapView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        initView();
    }

    private void initView() {
        mapView = (MapView)findViewById(R.id.mymapview);

//        mapView.setTileSource(TileSourceFactory.MAPNIK);
        mapView.setDrawingCacheEnabled(true);
        mapView.setMaxZoomLevel(20);
        mapView.setMinZoomLevel(6);
        mapView.getController().setZoom(12);
        mapView.getController().setCenter(new GeoPoint(23.12645, 113.365575));
        mapView.setUseDataConnection(true);
        mapView.setMultiTouchControls(true);// 触控放大缩小
        //是否显示地图数据源
        mapView.getOverlayManager().getTilesOverlay().setEnabled(false);

        FragmentManager fm = this.getSupportFragmentManager();
        if (fm.findFragmentByTag("SampleGridlines") == null) {
            mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
            mSampleGridlines = new SampleGridlines();
            fm.beginTransaction().add(R.id.samples_container, mSampleGridlines, "SampleGridlines").commit();
        }
    }

}

这个类也是osmdroid里面示例代码来的。我稍微修改了一下。

package com.osmdroid.sample.util;

import android.graphics.Color;

import com.osmdroid.sample.util.MapViewBaseSampleFragment;

import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent;
import org.osmdroid.events.ZoomEvent;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.FolderOverlay;
import org.osmdroid.views.overlay.gridlines.LatLonGridlineOverlay;

/**
 * An example on how to use the lat/lon gridline overlay.
 *
 * basically, listen for map motion/zoom events and remove the old overlay, then add the new one.
 * you can also override the color scheme and font sizes for the labels and lines
 */
public class SampleGridlines extends MapViewBaseSampleFragment implements MapListener {

    FolderOverlay activeLatLonGrid;

    @Override
    public String getSampleTitle() {
        return "Lat/Lon Gridlines";
    }

    @Override
    protected void addOverlays() {
        super.addOverlays();
        LatLonGridlineOverlay.setDefaults();
        mMapView.setMaxZoomLevel(20);
        mMapView.setMinZoomLevel(6);
        mMapView.getController().setZoom(12);
        mMapView.getController().setCenter(new GeoPoint(23.12645, 113.365575));
        mMapView.setTilesScaledToDpi(true);
        mMapView.setMapListener(this);
        LatLonGridlineOverlay.fontSizeDp=16;
        LatLonGridlineOverlay.fontColor= Color.argb(255,0,255,0);
        LatLonGridlineOverlay.backgroundColor=Color.BLACK;
        LatLonGridlineOverlay.lineColor=LatLonGridlineOverlay.fontColor;
        updateGridlines();
    }

    @Override
    public boolean onScroll(ScrollEvent scrollEvent) {
        updateGridlines();
        return false;
    }

    @Override
    public boolean onZoom(ZoomEvent zoomEvent) {
        updateGridlines();
        return false;
    }

    protected void updateGridlines(){

        if (mMapView==null)
            return; //happens during unit tests with rapid recycling of the fragment
        if (activeLatLonGrid != null) {
            mMapView.getOverlayManager().remove(activeLatLonGrid);
            activeLatLonGrid.onDetach(mMapView);
        }
        LatLonGridlineOverlay.backgroundColor= Color.BLACK;
        LatLonGridlineOverlay.fontColor= Color.BLUE;
        LatLonGridlineOverlay.lineColor= Color.BLUE;
        activeLatLonGrid = LatLonGridlineOverlay.getLatLonGrid(getActivity(), mMapView);
        mMapView.getOverlays().add(activeLatLonGrid);

    }

    @Override
    public void onDestroyView(){

        if (activeLatLonGrid!=null)
            activeLatLonGrid.onDetach(mMapView);
        activeLatLonGrid=null;
        super.onDestroyView();
    }

}

还有这个类的父类我就不贴出来。感兴趣下载示例代码以及apk对照。很多东西多看几眼就明白了。接下来说加载map格式地图

3,map与mapsforge

相关的库

    compile(name: 'osmdroid-android-5.6.3', ext: 'aar')
    compile files('libs/mapsforge-map-android-0.6.1.jar')
    compile files('libs/mapsforge-map-0.6.1.jar')
    compile files('libs/mapsforge-core-0.6.1.jar')
    compile files('libs/mapsforge-map-reader-0.6.1.jar')


还有这三个类


在osmdroid 示例代码也是可以找到的,或者jar包。

            //加载map地图格式
            if(fileName.contains(".map")){
                File[] mapfile = new File[1];
                mapfile[0] = exitFile;
                XmlRenderTheme theme = null;
                try {
                    theme = new AssetsRenderTheme(context, "renderthemes/","rendertheme-v4.xml");
                    AndroidGraphicFactory.createInstance(context.getApplication());
                }catch (Exception ex){
                    ex.printStackTrace();
                    return;
                }
                MapsForgeTileProvider forge = new MapsForgeTileProvider(new SimpleRegisterReceiver(context),
                        MapsForgeTileSource.createFromFiles(mapfile,theme, "rendertheme-v4"),null);
                mapView.setTileProvider(forge);
                return;
            }
里面涉及到一个assets目录下建一个文件夹renderthemes,把rendertheme-v4.xml拷贝进去,相当于一个读取默认样式的。

最后嘱咐加上权限,联网以及读取目录的。

4,附上几张图,多了解一下





资源下载:后面上传之后再更新。

本博客源码下载:http://download.csdn.net/detail/qq_16064871/9763983



评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值