6

Ok. You'd think this would be relatively simple, but nope.

I am using Open street maps on my website, as the data is free to use, edit, and update - my project follows this mantra, and alongside googles API usage restrictions/saving data restrictions, google maps simply was not suitable for me.. at least on the web.

Given this I thought i'd follow suit with my android app, and so I setup osmdroid utilizing open street maps data.

I can have a nice map in my activity.. I also followed a tutorial, and it seemed to work at least such that I can have a map view in a fragment within a tabhost/viewpager. What i really want however (for tablets), is a list of establishments in a fragment on the left, and a map fragment on the right showing where these establishments are.

Simple enough i would have thought, but hours and hours of research suggest this is not possible unless you use some complex code and some deprecated methods..

So.. the fragment (containing a map view) loads and runs perfectly in a tabhost.. i can access methods within the fragment from my activity etc.. yet nothing to just simply have two fragments side by side.

Now.. I know google have just come out with their API v2.. I installed it and had a play.. not really knowing how osmdroid works, I thought i could update so I have MapFragment instead of SherlockFragment (I use ABS).. this then just threw up logcat errors asking for API keys etc.. and given that I dont want to use google map data, I assumed that I had gone wrong.

So.. could anyone advise on how I can get a list fragment, and a map fragment side by side using anything that is available on the market, but preferably utilizing Open source map data such that their are no usage restrictions.

I'm sure an overall overview of "what is available, and how it works" would be very much appreciated by loads of users.. so if anyone could advise it would be amazing !

4

2 回答 2

11

在新的 Google Maps API 出现之前,我被迫使用 OSMDroid 来实现我的目的。然而,在不得不多次挖掘源代码以弄清楚它为什么会这样做(通常是错误的)之后,我渴望一个新的库。

幸运的是,新的 Google Maps API 符合要求。您可以在您的应用程序中使用 Google Maps API v2,而不必使用 Google 的底图,但是是的,很遗憾,您必须先获取 API 密钥。

我假设您想使用 OpenStreetMap 作为在线地图源,因此下面的代码将为此量身定制。

GoogleMap map = <get-map-instance>;

map.setMapType(GoogleMap.MAP_TYPE_NONE);

TileOverlayOptions options = new TileOverlayOptions();

options.tileProvider(new UrlTileProvider(256, 256) {
    @Override
    public URL getTileUrl(int x, int y, int z) {
        try {
            String f = "http://tile.openstreetmap.org/%d/%d/%d.png";
            return new URL(String.format(f, z, x, y));
        }
        catch (MalformedURLException e) {
            return null;
        }
    }
});

map.addTileOverlay(options);

您可以在我的 Google Maps API v2 附加组件库中找到此代码的更正式版本

此外,如果您的目标是 pre-Fragment Android 版本,我相信您是基于您使用 ABS,而不是使用 MapFragment,请确保您使用 SupportMapFragment。

于 2012-12-16T00:25:18.530 回答
3

The Sample Code delivered with Google Maps Android API v2 contains the class com.example.mapdemo.TileOverlayDemoActivity. I little update to this class will show OpenStreetMap tiles within the new v2 framework. Cool! No need to use osmdroid or sherlock-fragments anymore. However... the tiles are not cached :(

Anyway, all additions/updates to the TileOverlayDemoActivity class are shown below:

private static final String OPEN_STREET_MAP_URL_FORMAT =
        "http://tile.openstreetmap.org/%d/%d/%d.png";

private void setUpMap() {
    mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
    mMap.setMyLocationEnabled(true);

    TileProvider tileProvider = new UrlTileProvider(256, 256) {
        @Override
        public synchronized URL getTileUrl(int x, int y, int zoom) {
            String s = String.format(Locale.US, OPEN_STREET_MAP_URL_FORMAT, zoom, x, y);
            URL url = null;
            try {
                url = new URL(s);
            } catch (MalformedURLException e) {
                throw new AssertionError(e);
            }
            return url;
        }
    };

    mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
}
于 2012-12-28T21:37:40.910 回答