Wednesday 23 January 2013

Google Maps in Android


Hi All,
              
                    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.
Step 1:  create an android project in Eclipse, instead of choosing the target name as Android 2.0, you need to select the target as Google APIs.
Step2: The Activity class you create this time should not extend the standard “Activity” class but should extend the MapActivity class.
Step3: u need to add the following tag in the AndroidManifext.xml in order to use the external library and permissions:
<uses-library android:name="com.google.android.maps" />

<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>

Step 4: 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"
    />

This is for sample java code here :
public class SampleMapActivity extends MapActivity {      
          MapView    myMapView        = null;
          MapController     myMC        = null;
          GeoPoint    geoPoint = null;
          double        latitude       = 17.3667, longitude = 78.4667;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myMapView = (MapView) findViewById(R.id.myGMap);
   geoPoint = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
          myMapView.setSatellite(false);
          myMC = myMapView.getController();
          myMC.setCenter(geoPoint);
          myMC.setZoom(15);
          myMapView.setBuiltInZoomControls(true);
       myMapView.displayZoomControls(true);
    }
@Override
protected boolean isRouteDisplayed() {
          return false;
}
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;
          }
          return false;
          }
}

Obtaining a Google Maps Android v1 API Key:

Step1: Locate debug.keystore on your system. It is usually in theC:\Users\Sreeni\.android\debug.keystore”.

Step2: Use the keytool utility to generate certificate fingerprint (MD5). keytool utility that comes with the default JDK installation.
Simply go to the command prompt:
C:\Users\Sreeni>cd C:\Program Files\Java\jdk1.7.0\bin
Then press enter…….
C:\Program Files\Java\jdk1.7.0\bin>
After  adding like this….
C:\Program Files\Java\jdk1.7.0\bin>keytool -v -list -alias androiddebugkey -keys
tore C:\key\debug.keystore -storepass android -keypass android
You get the MD5 ..Certificate fingerprints:
         MD5:  46:78:01:3F:8E:B0:C0:71:EF:04:A6:85:C9:70:75:3E
         SHA1: 25:68:CE:BD:6E:C4:04:DA:87:67:CF:9F:B3:D9:A0:99:5A:89:FD:DC
         SHA256: 6B:8F:FD:73:B6:62:33:68:D3:83:23:90:75:6D:40:C8:2A:3E:29:4A:FE:
44:CA:45:01:B6:F7:29:CE:5B:0A:86


Step3 : Go to the “ https://developers.google.com/maps/documentation/android/v1/maps-api-signup “. Put your Certificate fingerprint (MD5) And get your API key for android Google Map application. On this page, paste the certificate fingerprint in the appropriate form field, and click the “Generate API Key” button. This will return a Maps API key string.

This key needs to be used along with the MapView element declared in the XML layout file as mentioned in the above
      android:apiKey="0YaGLMeMFKXrhzHL-uSYZSnqXqbGwE6kxF4VwFQ".



1 comment:

  1. Hey this process of Google maps integration is out dated. Google maps introduced new Api version v2.

    https://developers.google.com/maps/documentation/android/

    Please go through the link thy provided full documentation.

    ReplyDelete