📜 ⬆️ ⬇️

Integration of maps into your Android application

There are many different ideas for the implementation of which you may need maps on mobile devices.
The capabilities of such devices allow quite quickly obtain location information, graphic data and graphic transformations for displaying the landscape size.

In this article I will explain how to integrate the ability to view maps for mobile devices based on Android, using the example of Yandex Maps and Google Maps.
The functional library of these companies allows you to:



Let's start with the domestic manufacturer.
')

Yandex


The Yandex MapKit library can be downloaded from GitHub , there is also a version for iOS.
But before we start using, we need to get an API-key, for this we go to the post office and write a letter to support@mobmaps.yandex.ru , indicating the API key request in the “Map Kit” header. In response, you will be sent a letter indicating additional references to the documentation, to the terms of use and, in fact, with the key itself in the body of the letter (or they may not send it, by the way).

While we wait for the letter, we take the library and attach it to the project.

After a long wait with a cup of coffee, we 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.MapView android: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. Everything, the card is.

Next, the code shows how to move the map to a given coordinate and use the zoom:
 final MapView mMapView = (MapView) findViewById(R.id.map); //  MapController MapController mMapController = mMapView.getMapController(); //      mMapController.setPositionAnimationTo(new GeoPoint(60.113337, 55.151317)); mMapController.setZoomCurrent(15); 


Google


To use Google Maps you need to take much more sophisticated actions. I'll tell you how to get a debug-key to use the cards, but the process of getting a release version seems a bit more confusing, but I haven't received a release yet.

To get a debug-key, you need to find your keystore for debugging.
By default, the path will look something like this:
 C:\Documents and Settings\<user>\.android\debug.keystore 

Then open the command line, go to the directory where Java is installed:
 cd C:\Program Files\Java\jre6\bin 

Then we write:
 keytool -list -alias androiddebugkey -keystore <___>.keystore -storepass android -keypass android 

where path_to_to_key_stores is your way to debug.keystore (be careful, there may be problems with spaces on the way).

After that, the md5 key after the “Certificate fingerprint (MD5)” will be displayed in the same window.
We follow the link and register the code.
Then Google will generate with the necessary code for the layout and the embedded code, take it and paste it into our layout.
Next, we need to present our Activity in the following form:
 package com.fewpeople.geoplanner; import android.os.Bundle; import com.google.android.maps.MapActivity; public class GeoMapActivity extends MapActivity { @Override public void onCreate(Bundle savedInstanceData) { super.onCreate(savedInstanceData); setContentView(R.layout.geomap); } @Override protected boolean isRouteDisplayed() { return false; } } 


I note that the Activity must be inherited from MapActivity stored in com.google.android.maps.MapActivity.
And in the manifest to register between tags
 <application></application> 
insert:
 <uses-library android:name="com.google.android.maps" /> 

And do not forget about allowing access to the Internet for the application:
 <uses-permission android:name="android.permission.INTERNET" /> 


The rest of GoogleMaps and YandexMapKit are very similar:
 final MapView mMapView = (MapView) findViewById(R.id.mapview); //  MapController MapController mMapController = mMapView.getController(); //      mMapController.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 use is quite wide.
More details can be found below.

Determination of own position


To expand the example a bit, I'll attach a piece of code:
 final MapView cMapView = (MapView) findViewById(R.id.map); //  MapController MapController cMapController = cMapView.getMapController(); //   ,   cMapView.setBuiltInZoomControls(true); //     "" final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); final LocationListener locationListener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) {} @Override public void onProviderEnabled(String provider) {} @Override public void onProviderDisabled(String provider) {} @Override public void onLocationChanged(Location location) {} }; locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); //  overlay     cMyLocationOverlay = new MyLocationOverlay(this, cMapView); cMyLocationOverlay.disableCompass(); cMyLocationOverlay.enableMyLocation(); cMapView.getOverlays().add(cMyLocationOverlay); //  cMapController.setZoomCurrent(15); //    (  LocationManager.NETWORK_PROVIDER,     ) Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //      cMapController.animateTo(new GeoPoint((int) location.getLatitude() * 1000000, (int) location.getLongitude() * 1000000)); 


But to use this code, the following permission should be specified in the manifest:
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 




List of material used:
Hello, MapView (developers.android.com)
Google Maps Api receipt (temporary)
Getting the Google Maps API Key
Maps in your Android application
We write a simple application using GoogleMap and pumped SimpleCursorAdapter
Terms of use of the Yandex Map Kit service
Yandex Map Kit for Android OS in examples (Maxim Khromtsov)
Maxim Khromtsov. Yandex MapKit for Android OS in examples (Video)

Source: https://habr.com/ru/post/131252/


All Articles