📜 ⬆️ ⬇️

Let's taste API v2 maps for Android from Google

And on our street trucks with cookies are being turned over ice cream and marmalades! As already mentioned , a new version of the API has been released. And even in brief it was told what was new there. I, while, cannot uncover all improvements, from the point of view of the developer. But some special advantages of developing applications using maps are ready to be highlighted.

The first, and probably the most delicious, is now possible to use fragments at all, instead of MapView. To do this, simply insert the following code into the markup:

<fragment android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment"/> 

Secondly, it has become much more convenient to work with pins. True, now you need to get used to the fact that they are called markers, and it will be much more convenient. Now you do not need to fence ItemizedOverlay, and the like. Now it is enough just to execute the addMarker (MarkerOptions options) method with the necessary parameters, and the marker will show off at the specified position.
')
The third plus is that it has become absolutely convenient to draw objects on the map that will easily zoom in and scroll along with the map, without any problems! Simply add a shape (the addPolygon method (PolygonOptions options) ) or a line (the addPolyline method (PolylineOptions options) ) to the map, that's all. This means that now you can easily paint over the country, city, district ... Your car?) Yes, whatever!



The following minuses rushed into my eyes:

  1. Now, to work with maps, the user must have the Maps application from Google itself installed. Otherwise, an error is spilled into the log that "Google Maps application is missing". But the application does not tell the user about it, which can annoy him. Probably, you can make a crutch, but it is a pity that it is not immediately. PS: I have CyanogenMod on the device, and I caught the error.
  2. It is also impossible to do without the application “Google Play Services”. True, this problem will be reported to the user .., but not in a very beautiful form. I have not yet looked for ways to make this message more appealing.
  3. It seems that the move to the new version of the API will be unnecessarily laborious, which is a little depressing. Especially if a lot has already been done, but not a little work remains to be done, and new chips are just what you need.
  4. Of course, it may be critical for someone that is compatible only with Android 2.2+


Do not scare? Then proceed to launch our first example.

First we need to install / update the Google Play services SDK. To do this, do the following:


After the installation is complete, you can find the libproject / google-play-services_lib and samples / maps folders at <android-sdk> / extras / google / google_play_services. From the contents of the first folder you need to make a module, which we will then connect to the project. And from the second - just an example of an application for working with maps. I advise you to first copy these folders to the directory where your projects are located, and already work on them there, so that the SDK always contains a clean copy of the library with examples.

Unfortunately, I cannot tell how to check all this in Eclipse. But cheat sheet how to do it in Intellij Idea bring.
Deploying the sample project in Intellij Idea
  • File → New Project ...
  • Create project from existing sources → Next
  • Install the full path to samples / maps in the Project files location and click Next, Next ...
  • File → Project Structure → Modules → Module SDK: Android 4.2 Google APIs → OK
  • In google-play-services_lib / src, create a com / google / Hack.java file with something like this:
     package com.google; public class Hack { // Empty class to ensure there is a src folder. } 

    It is necessary for Idea to normally create a module from this directory.
  • Copy the android-support-v4.jar to the google-play-services_lib / libs folder. It will be required for compatibility with older versions of Android.
  • File → Project Structure → Modules → “Plus” above the second column → Module
  • Create module from existing sources → Specify the path to google-play-services_lib → Next, Next ...
  • On the right, click “Plus sign” → Jars or directories → google-play-services_lib / libs → Ok
  • Put a tick next to the line that appears in the list
  • In the second column, select another module (respectively, the project) → “Plus” on the right → Module Dependency ... → OK → OK


Startup problems have happened, but they were pretty minor.

To earn the cards, it remains to receive a new API_KEY - the old one is no longer suitable. I will allow myself to describe this process in more detail, which in brief is uv. djvu explained here :

  1. Create SHA1 fingerprint from your debazh or release key
  2. We go to the console
  3. Create project ... → Enter the name of the project, for example “Google Maps Android API v2 test project”
  4. Switch Google Maps Android API v2 translate from “off” to “on”
  5. We agree with “terms” (otherwise, we say goodbye to Google Maps Android API v2).
  6. In the left navbar, go to the API Access menu and click the “Create new Android key ...” button located below.
  7. Enter “<SHA1>; <package name>”. In my case, the package name was com.example.mapdemo. If you are not sure of the name of your package, you can easily look at it in the AndroidManiffest.xml file created during application creation, in the first manifest tag, the value of the package attribute.
  8. We press the button Create.
  9. The resulting key is inserted into AndroidManifest.xml
     <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your_api_key"/> 

  10. Run!


In the resulting application will be a lot of interesting things, I advise you to study as thoroughly as possible.

PS: all the main documentation on the new API is collected here .

Easy and successful geocoding you!

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


All Articles