Sabtu, 29 Oktober 2016

All About Android-Google Map in Android Application - Sample Code-ANDROIDKAWE


Sample Code - Google Map in Android Application


Location aware applications on android platform needs an intuitive way to present the location information.
Google provides a set of APIs to integrate the Google Map for development of android applications and bundled as Google APIs.

This post is focusing on Google Map object integration with android application.

Create a Android application project "GMapSample" and open the default class file generated in the src directory as the main activity class. See here the project creation dialog

Eclipse Android Project
Create New Android Application Project Using Eclipse
To create the Google Map in the application you need to select the Build SDK as Google APIs (API15). If this is not available then you need to update the SDK and download using Android SDK Manager.

The following includes need to add in order to integrate the Google Map with your application.

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

The MapActivity extends the basic Activity class of Android and provides the Google Map as the user interface.

In order to access the Google Map in your application you need to get a Application Key and define the Google Map object in your application XML layout.

 <com.google.android.maps.MapView
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0HvaRYYCBNDap4SKY0HN-XXXXXXXXXXXXXXXX"
/>

To get the API key for your application see the following link.

http://creativeandroidapps.blogspot.in/2012/07/get-application-key-for-google-map.html

Now replace the code of main activity class by following code

package com.example.gmapsample;

import android.os.Bundle;

import android.view.Menu;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;


public class GMapSample extends MapActivity{

private static final String TAG = "GMapSample";
private MapView mapView;
private MapController mapController;
private static final int latitudeE6 = 17421306;
private static final int longitudeE6 = 78457553;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gsample);
mapView = (MapView) findViewById(R.id.map_view); //get the map view resource added in XML file
mapView.setBuiltInZoomControls(true);
GeoPoint point = new GeoPoint(latitudeE6, longitudeE6); //defines the geo location
mapController = mapView.getController(); //get the map controller
mapController.animateTo(point); // show the location defined by geopoint
mapController.setZoom(7); //setting the map zoom level
mapView.invalidate(); //redraw the map

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_gsample, menu);
return true;
}

@Override
protected boolean isRouteDisplayed() {
return false;
}
}

while compiling the application you will get some error because we have not added the use of google APIs in AndroidManifest.xml file of the application.

Add the following line in your application manifest file.

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

Add the following permissions also to manifest file.


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



To run the application you need to define a AVD from the AVD Manager as follows

AVD Manager
AVD for Google Map

Running the application will give the following output.

Google Map Android
Android Application with Google Map
Please provide your feedback. The next post for the Google Map is on the way.

Thanks
Creative Android Apps



Previous Post
Next Post

0 komentar: