Integrating Open Street Map in Android application
Open Street MapIS GOOGLE MAP DEPRECATED?
Basic Ideas Open Street Map (OSM) is a free editable map. The maps are created using data from portable SAT NAV devices, aerial photography & simply from local knowledge. The real power of OSM is not default rendering, but the possibility to actually access the data behind this map rendering. And this is where it differs from Google maps, Bing Maps and Yahoo! Maps and various others. Let’s have a look to OSM at http://www.openstreetmap.org/
Why we are interested? From 1 January 2012, Google will charge for the Google Maps API service when more than the limit of 25,000 map "hits" are made in a day. Google is rumored to be charging $4 per 1,000 views in excess of the limit. These two points may be good enough to make us interested for hunting a free but reliable option.
Getting StartedLets Create a new Project in eclipse.
Getting StartedWe have to include two jar files. These are: 1. osmdroid-android-1.04.jar which can be downloaded from http://www.java2s.com/Code/Jar/o/Downloadosmdroidandr oid104jar.htm 2. slf4j-android-1.5.8.jar which can be downloaded from http://www.slf4j.org/android/ Download the jars by clicking the links above. Create a folder libs into the project & put thejars in the libs folder.
Add jars Add the jars by configuring java build path: Right click on project>Buils Path>Configure Build Path > Libraries > Add Jars & then select the jars from theProjects libs folder.
Add permissionsAdd the permissions to the manifest. <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" />
Override onCreate Now we are ready for some java coding. We Override the onCreate method of MyOpenStreetMapActivity:
Analyze onCreateLets analyze the onCreate method:1. We set the content view to main.2. We initialize the relative layout.3. We initialize the map view.4. We add the built in zoom control.5. We initialize the MyLocationOverlay Class with the context & mapview.6. We add the overlay to our map view.7. We add the map view to our relative layout.8. We set the zoom level.
Override onResume & onPause We override the onResume Method & myLocationOverlay is enabled to read the GPS location: We override the onPause method to disable the location listener:
Deployment Now we are ready to run our application. Here we are seeing a buttonNamed “Google Map”. LetsImplement some functionality toIt. Implement a google map tothis button which will toggle theMap to google map & shows theuser’s current location.