如何创建从OpenStreetMap的离线地图瓦片,在Android上显示呢? [英] How to create map tiles from OpenStreetMap offline, display it on Android?

查看:608
本文介绍了如何创建从OpenStreetMap的离线地图瓦片,在Android上显示呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的是显示使用OpenStreetMap的一个简单的离线地图。我找不到在Web合适的工具来创建地图图块,并用它来显示Android的地图。我已经下载了不同的资源,但似乎我没有任何想法从哪里开始。我想用集成JOSM从OpenStreetMap的图像,但我不知道我是否可以在Android上使用它。

What I want is to display a simple offline map using OpenStreetMap. I cannot find in the web the right tools to create map tiles and use it to display a map in Android. I have downloaded different resources but it seems that I don't have any idea where to start. I want to integrate images from OpenStreetMap using JOSM but i don't know if I can use it on Android.

我可以使用Mapnik的?您的帮助会很大的感谢你。

Can I use Mapnik? Your help will a great thank you.

推荐答案

我目前正在开发(我第一次)使用OpenStreetMap的(OSM)API,因此,虽然我不能帮你的JSOM Android应用程序,我可以尽力帮助与OSM部分:

I'm currently developing (my first) Android application using the OpenStreetMap (OSM) API, so while I can't help you with the JSOM, I can try to help with the OSM part:

假设你想创建你的android应用程序中的新的活动,仅仅显示一个OSM的地图,你可能会开始是这样的:

Assuming that you want to create a new activity in your android application that simply displays a OSM map, you might start with something like this:

package example.stackoverflow.osmdroid;

import android.app.Activity;
import android.os.Bundle;

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

public class YourMap extends Activity {
    // The MapView variable:
    private MapView m_mapView;

    // Default map zoom level:
    private int MAP_DEFAULT_ZOOM = 15;

    // Default map Latitude:
    private double MAP_DEFAULT_LATITUDE = 38.535350;

    // Default map Longitude:
    private double MAP_DEFAULT_LONGITUDE = -121.753807;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Specify the XML layout to use:
        setContentView(R.layout.osm_map);

        // Find the MapView controller in that layout:
        m_mapView = (MapView) findViewById(R.id.mapview);

        // Setup the mapView controller:
        m_mapView.setBuiltInZoomControls(true);
        m_mapView.setMultiTouchControls(true);
        m_mapView.setClickable(true);
        m_mapView.setUseDataConnection(false);
        m_mapView.getController().setZoom(MAP_DEFAULT_ZOOM);
        m_mapView.getController().setCenter(
            new GeoPoint(MAP_DEFAULT_LATITUDE, MAP_DEFAULT_LONGITUDE));
        m_mapView.setTileSource(TileSourceFactory.MAPNIK);
    } // end onCreate()
} // end class YourMap

如果您osm_map.xml布局可能会是这个样子:

Where your osm_map.xml layout may look something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <org.osmdroid.views.MapView
            android:id="@+id/mapview"
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
            android:enabled="true"      
            android:clickable="true"
        />  
</RelativeLayout>

至于实际的地图图块,有一个叫很酷程序的移动阿特拉斯造物主的,它可以生成必要的地图图块的离线Android的地图上面实现。

As for the actual map tiles, there is a really cool program called Mobile Atlas Creator, which allows you to generate the necessary map tiles for the offline Android map implemented above.

一旦你安装的应用程序,您要创建一个新的地图,你会被要求选择你的所需的阿尔塔斯格式。当此弹出,选择Osmdroid拉链。

Once you have the application installed, and you want to create a new atlas, you'll be asked to select your "desired altas format." When this pops up, select "Osmdroid zip."

在所有负载,在地图上选择一个区域,你想创建瓷砖,选择您想要瓷砖的缩放级别,并点击添加选择按钮,在左边的列,其次是创建阿特拉斯。

Once everything loads, select a region on the map that you would like to create tiles for, select the zoom levels you want tiles for, and hit the "Add Selection" button in the column on the left, followed by "Create Atlas."

哦,根据来源,您可能需要选中创建/调整地图图块复选框来强制导出为PNG格式的瓷砖 - 没有人知道是否有可能使用JPG与OSM?

Oh, and depending on the source, you may need to check the "Create/Adjust Map tiles" checkbox to force the tiles to be exported as PNGs -- does anyone know if it's possible to use JPG with OSM?

在该ZIP已经产生,我给它改名为Mapnik.zip,并把它移到一个在我的Eclipse Android项目工作区被称为砖新建文件夹。为了得到它的工作,我也有过,以便打开压缩文件,并重新命名的类似谷歌地球的顶层文件夹(这取决于你使用的地图源),以Mapnik的,为瓦在我的Andr​​oid应用程序的显示。

Once the ZIP has been generated, I renamed it to "Mapnik.zip" and moved it to a newly created folder called "tiles" in my Eclipse Android project workspace. In order to get it working, I also had to open the zip file, and rename the top level folder from something like "Google Earth" (depending on the map source you used), to "Mapnik," in order for the tile to display in my Android application.

为了实际加载的瓷砖到您的手机,但是,你需要从终端使用ADB工具。在您的ADB工具的目录,你想运行这样的事情(每一行是一个新的命令):

In order to actually load the tiles onto your phone, however, you'll need to use the ADB tool from the terminal. In your ADB tool directory, you'll want to run something like this (each line is a new command):

./adb shell rm -r /sdcard/osmdroid/
./adb shell mkdir /sdcard/osmdroi/
./adb push ~/path/to/your/mapnik.zip /sdcard/osmdroid

根据地图的大小和手机的存储器总线的速度,这最后一步可能需要几分钟到一个小时内完成。一旦这样做,你的地图应该努力 - !我希望

Depending on the size of the map and the speed of the phone's memory bus, this last step may take a several minutes to an hour to complete. Once done, your map should work -- I hope!

正如我所说,这是我第一次用的OSM API,所以我绝不是它的专家,我可以为我工作仅仅注释。

As I mentioned, this is the first time I've used the OSM API, so I'm by no means an expert on it, and I can only comment on what worked for me.

希望这将帮助你开始!

编辑:

我没有机会实际运行code,我昨天写了,所以我没赶上了几个错误。我刚刚创建了一个新的项目在Eclipse中,甩我的code。在那里,定了一些东西,得到它运行起来。我的所有更改会反映在code以上!我忘了一些基本的import语句,我忘了权限添加到manifest.xml文件。

I didn't have a chance to actually run the code that I wrote up yesterday, so I didn't catch a few of the errors. I just created a new project in Eclipse, dumped my code in there, fixed a few things and got it up and running. All changes that I made are reflected in the code above! I forgot several of the basic import statements, and I forgot to add permissions to the manifest.xml file.

我的manifest.xml的最后几行现在看起来是这样的:

The last few lines of my manifest.xml now look like this:

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

和您可能还需要将它添加到清单,但肯定不是重要的:

And you also might want to add this to the manifest, although certainly not critical:

<supports-screens 
    android:anyDensity="true"
    android:resizeable="false"
    android:largeScreens="true"
    android:normalScreens="true"
/>

在添加这一权利的&LT;使用的SDK ... /&GT; 部分。

Add this right after the <uses-sdk ... /> part.

此外,一定要导入以下两个JAR库:

Furthermore, be sure to import the following two JAR libraries:

osmdroid-android-3.0.3.jar // Or whatever version you're using...

slf4j-android-1.5.8.jar // Or whatever the latest version is...

如果没有这最后的JAR,我的code保持崩溃,直到我记得把它列入。

Without this last JAR, my code kept crashing until I remembered to include it.

确认,使它们指向你实际有地图图块的位置,修改默认的坐标,否则你不会看到任何东西,除了一个白色的画布。

Make sure to modify the default coordinates so that they point to a location that you actually have map tiles for, otherwise you're not going to see much of anything, aside from a white canvas.

对不起,没有运行在我结束在code第一!

Sorry for not running the code on my end first!

这篇关于如何创建从OpenStreetMap的离线地图瓦片,在Android上显示呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆