osmdroid, Maps API v2, and fragments
Asked Answered
C

2

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 !

Cudlip answered 15/12, 2012 at 21:9 Comment(5)
There is no harm in getting an API key if your not going to use their data. Check out this overflow #13695893Continuate
It seems like the way to go, but im looking for a more in depth explanation as to its workings. e.g how would i implement a map with open source tiles in its simplest form? and as mentioned in that question - do api usage limitations still apply. Essentially i'd like a more in dept explanation using that question as a base.. Cheers.Cudlip
As it's early days with this new API, it's highly unlikely a guide/example/tutorial has been written about osm/osmdroid integration with it. This will probably change as time moves on though.Continuate
:) Yup - to be expected. Do you have any insight into how it can be executed?Cudlip
Sorry, I don't think I can help with that. Following the getting started instructions for the Maps API v2 developers.google.com/maps/documentation/android/start with the answer implementation in the thread I linked to would probably be your best bet.Continuate
C
11

Before the new Google Maps API came out, I was forced into using OSMDroid for my purposes. However, after having to dig through the source several times to figure out why it was doing what it was (often incorrectly), I was dying for a new library.

Fortunately, the new Google Maps API fits the bill. You can use the Google Maps API v2 in your application and never have to use Google's basemaps, but yes, unfortunately you must get an API key first.

I am assuming you want to use OpenStreetMap as an online map source, so the code below will be tailored towards that.

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);

You can find a more formalized version of this code in my Google Maps API v2 add-ons library here.

Also, if you are targeting pre-Fragment Android versions, which I believe you are based on you using ABS, instead of using MapFragment, make sure you use SupportMapFragment.

Crinose answered 16/12, 2012 at 0:25 Comment(5)
So.. its taken me 4 hours of playing - but this works :) Only thing wrong is addTileProvider should be addTileOverlay.. Could you perhaps elaborate on your osmdroid decision/issues? Also - I have OSM overlays, but the Google logo is still displayed in the corner. Do you happen to know what is required in terms of attribution to OSM/given that I am still using 'google maps'. Furthermore I would presume that am i still bound by their usage caps? negating one of the major benefits of OSM Regardless, very much appreciated - thanks so much !!Cudlip
The Google logo in my opinion is a bug, and I have filed an issue here: code.google.com/p/gmaps-api-issues/issues/detail?id=4648. In terms of why not OSMDroid, it basically boiled down to performance, bugs, and lack of documentation. If you want to know why yourself, dig through the source and see how many no-ops you find with a // TODO comment in them. ;)Crinose
Also have dealt with issues with OSMdroid and am considering this usage. How has this been working now that you've had some time?Examen
Using custom TileProvider and MAP_TYPE_NONE has one painful drawback. The tile's are not cached, so you get annoying blinking while zooming in - did anyone of you managed to create some kind of workaround for this problem?Preform
Using UrlTileProvider / create custom UrlTileProvider, of OpenStreetMap it is not showing tile above android 7.1.1. with map type MAP_TYPE_NONE. is there any solution?Lina
S
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));
}
Shanteshantee answered 28/12, 2012 at 21:37 Comment(1)
Hey @Shanteshantee I tried with your solution, it is working fine below 7.1.1 version but in above like Oreo/Pie it is not working it shows blank. Please help with any solution.Lina

© 2022 - 2024 — McMap. All rights reserved.