Thursday, January 26, 2017

Google Maps on Android Android Developer Tutorial Part 14


Maps-based and location-based services are compelling for mobile users. Hence let us explore the support for map services in the Android platform in this tutorial. In the next we will  build upon this and add location services too.


Map services are provided by an external library that includes the com.google.android.maps package. This library is not provided as part of the standard SDK. It is provided as a Google APIs add-on to the android SDK. For the convenience of developers, this add-on is provided as part of the SDK on the emulator.


Before I jump into the example, let me explain some fundamental concepts related to the Maps API.


NOTE: For using the Google Maps, you have to obtain a Maps API key. How to obtain the key is also described towards the end of this tutorial. This is Step 1.


Maps API add-on provides a MapActivityclass that extends the Activity class of Android. This helps is managing the lifecycle of a MapView object that we will be using later. Our main class in the application will be extending the MapActivity class. This class can be treated as the default activity class whenever we deal with the Maps API.


MapView– This is a class that extends the View and the ViewGroup classes of Android. It provides the basic functionality expected on a Map like responding to key presses or touch and allowing zooming etc. It also provides a controller object that helps us manipulate the Map view programmatically. We will see how, in the example later.


Now that we have understood the basics of the Maps API, let us jump into the example.


Step 2: To begin with, when you create an android project in Eclipse, instead of choosing the target name as Android 1.5, you need to select the target as Google APIs. This ensures that the project has the map API external libraries along with android 1.5 SDK.


Step 3: The Activity class you create this time should not extend the standard “android.app.Activity” class but should extend the MapActivity class about which I explained earlier.


Step 4: Apart from this, you need to add the following tag in the AndroidManifext.xml in order to use the external library:


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


Step 5: Now let us declare the map view in main.xml in layout folder. To do so, add this element in order to display the map using MapView.


<com.google.android.maps.MapView
android:id="@+id/myGMap"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:enabled="true"
      android:clickable="true"
      android:apiKey="0YaGLMeMFKXrhzHL-uSYZSnqXqbGwE6kxF4VwFQ"
    />


Here the apikey should be what was generated by you in an earlier step 1.


 Step 6: In the activity class I set the geo point on the map and set the satellite view to false.


      myMapView = (MapView) findViewById(R.id.myGMap);
      geoPoint = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
      myMapView.setSatellite(false);


Step 7: Now you are almost ready to go. But we will try to do some programtic handling of the mapview. For that we need to get a handle to the MapController object. It is done this way:


      myMC = myMapView.getController();
      myMC.setCenter(geoPoint);
      myMC.setZoom(15);

I also set the zoom controls to be displayed with this code.


      myMapView.setBuiltInZoomControls(true);
      myMapView.displayZoomControls(true);


Step 8: Add the following permissions in the AndroidManifest.xml in order to access the maps over the internet:


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


You could execute your program now to view the map with the fixed geopoint. 


Step 9: One last step – let us add some actions to the key presses for zooming in, zooming out, viewing the satellite view, normal map view


      public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_I) {
                  myMapView.getController().setZoom(myMapView.getZoomLevel() + 1);
                  return true;
            } else if (keyCode == KeyEvent.KEYCODE_O) {
                  myMapView.getController().setZoom(myMapView.getZoomLevel() - 1);
                  return true;
            } else if (keyCode == KeyEvent.KEYCODE_S) {
                  myMapView.setSatellite(true);
                  return true;
            } else if (keyCode == KeyEvent.KEYCODE_M) {
                  myMapView.setSatellite(false);
                  return true;
            }<

Related Posts by Categories

0 comments:

Post a Comment