Home Development for Android Integrating maps into your Android app

Integrating maps into your Android app

by admin

There are many different ideas for which you might need maps on mobile devices.
The capabilities of such devices allow you to get location information, graphical data and graphical transformations to display the volume of the landscape quite quickly.
In this article I will explain how to integrate map browsing capability for Android-based mobile devices, using YandexMaps and GoogleMaps as examples.
The functionality of these companies’ libraries allows :

  • Move the map
  • Change scale
  • Change map views
  • Get card events

Integrating maps into your Android app
Let’s start with the domestic manufacturer.

Yandex

Library Yandex MapKit can be downloaded from GitHub , there is also a version for iOS.
But before you start using it, we need to get the API-key. support@mobmaps.yandex.ru with "Map Kit API key request" in the title.You will receive an email with more links to documentation, rules of usage and the key itself in the body of the letter (or may not send it, by the way).
While we wait for the letter, we take library and attach it to the project.
After a long wait with a mug of coffee, get acquainted with the contents of the letter and the information on the links, take the key and write the following code in your application layout :

<ru.yandex.yandexmapkit.MapViewandroid:id="@+id/map"android:layout_width="fill_parent"android:layout_height="fill_parent"android:apiKey="you are key" />

where instead of "you are key" insert the received key. That’s it, we have the card.
The following code shows how to move the map to a given coordinate and use zoom :

final MapView mMapView = (MapView) findViewById(R.id.map);// Getting a MapControllerMapController mMapController = mMapView.getMapController();//move the map to the given coordinatesmMapController.setPositionAnimationTo(new GeoPoint(60.113337, 55.151317));mMapController.setZoomCurrent(15);

Google

To use Google Maps you have to do much more sophisticated things. I’ll tell you how to get the debug-key to use the maps, but the process of getting the release version seems to be a bit more complicated, but I haven’t gotten the release yet.
To get the debug-key, you need to find your key repository, for debugging.
By default, the path will look like this

C:\Documents and Settings\<user> \.android\debug.keystore

Then open a command line and go to the directory where Java is installed:

cd C:\Program Files\Java\jre6\bin

Then we write:

keytool -list -alias androiddebugkey-keystore <path to key storage>.keystore-storepass android-keypass android

where path_to_the_keys_store – is your path to debug.keystore (be careful, there may be problems with spaces in the path).
After that in the same window the md5-key will be displayed after "Certificate fingerprint (MD5)"
Follow this link and register the code.
After that Google will generate with the right code for layout and embed code, take it and paste it into your layout.
Next, we need to represent our Activity in about the following way :

package com.fewpeople.geoplanner;import android.os.Bundle;import com.google.android.maps.MapActivity;public class GeoMapActivity extends MapActivity {@Overridepublic void onCreate(Bundle savedInstanceData) {super.onCreate(savedInstanceData);setContentView(R.layout.geomap);}@Overrideprotected boolean isRouteDisplayed() {return false;}}

Note that the Activity must be inherited from MapActivity stored in com.google.android.maps.MapActivity.
And in the manifest, put between the tags

<application> </application>

paste :

<uses-library android:name="com.google.android.maps" />

And don’t forget, Internet access permission for the app :

<uses-permission android:name="android.permission.INTERNET" />

Otherwise, the functionality of GoogleMaps and YandexMapKit is very similar :

final MapView mMapView = (MapView) findViewById(R.id.mapview);// Getting a MapControllerMapController mMapController = mMapView.getController();// Moving the map to the specified coordinatesmMapController.animateTo(new GeoPoint(60.113337, 55.151317));mMapController.setZoom(15);

As you can see, the process of integrating maps into applications is quite simple, and the range of uses is quite wide.
More details can be found below.

Defining one’s own position

To extend the example a bit, I’ll attach a piece of code :

final MapView cMapView = (MapView) findViewById(R.id.map);// Getting a MapControllerMapController cMapController = cMapView.getMapController();// Enabling interface elements that control the zoom ofcMapView.setBuiltInZoomControls(true);// Creating a location manager and a listener;final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);final LocationListener locationListener = new LocationListener() {@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}@Overridepublic void onProviderEnabled(String provider) {}@Overridepublic void onProviderDisabled(String provider) {}@Overridepublic void onLocationChanged(Location location) {}};locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);// creating an overlay to display our positioncMyLocationOverlay = new MyLocationOverlay(this, cMapView);cMyLocationOverlay.disableCompass();cMyLocationOverlay.enableMyLocation();cMapView.getOverlays().add(cMyLocationOverlay);// zoomcMapController.setZoomCurrent(15);// Get your coordinates (you can use LocationManager.NETWORK_PROVIDER, for a different way to determine position)Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);// Moving the map to the specified coordinates cMapController.animateTo(new GeoPoint((int) location.getlature() * 1000000, (int) location.getLongitude() * 1000000));

But to use this code, the following permission must be specified in the manifest :

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Integrating maps into your Android app
List of material used :
Hello, MapView (developers.android.com)
Google Maps getting Api (temporary)
Getting Google Maps API Key
Maps in your Android app
Writing a simple application using GoogleMap and a pumped-up SimpleCursorAdapter
Terms of Use of "Yandex Map Kit" service
Yandex Map Kit for Android OS in examples (Maxim Khromtsov)
Maxim Khromtsov. Yandex MapKit for Android OS in examples (Video)

You may also like