Saturday, March 24, 2012

Map Example


Description:
This example of will show how we can show and use google map into our application.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it MapExample.
2.) You will see some default code into your main.xml, strings.xml and android manifest file.
3.) Now create your MD5 fingerprint and map API key. To generate MD5 fingerprint open command prompt and change the directory to your jre path. Run keytool command to check the path is correct or not.
Enter following command: keytool -list –v -alias androiddebugkey -keystore “ .keystore” -storepass android -keypass android
Check http://code.google.com/intl/pl/android/add-ons/google-apis/mapkey.html to get more information.
4.) Add following library and permissions to your manifest file:
<uses-library android:name="com.google.android.maps" />
<uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_GPS" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Steps:
1.) Create a project named MapExample and set the information as stated in the image.
Build Target: Android 2.1
Application Name: MapExample
Package Name: com.org. MapExample
Activity Name: MapExample
Min SDK Version: 7
2.) Open MapExample.java file and write following code there:
package com.org.MapExample;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
public class MapExample extends MapActivity {

        /** Called when the activity is first created. */
         private Location myLocation;
     protected MapView myMapView = null;
     protected LocationManager myLocationManager = null;
     protected MapController mapController;
     List<Overlay> mapOverlays;
     @Override
     protected boolean isRouteDisplayed() {
             // TODO Auto-generated method stub
             return false;
     }
     @Override
     protected void onCreate(Bundle icicle) {
             // TODO Auto-generated method stub
             super.onCreate(icicle);
             this.myMapView = newMapView(this,"0jp8vWjNayJISFKdvcJwGmwsjgoCoQrT_dflCfQ");
             this.setContentView(myMapView);
             myMapView.setBuiltInZoomControls(true);
             myMapView.setSatellite(true);
                mapController = myMapView.getController();
                mapOverlays = myMapView.getOverlays();
                this.myLocation = new Location("gps");
            this.myLocation.setLongitude(77.52436144125092);
            this.myLocation.setLatitude(13.05096452223662);
             updateView();
     }
     private void updateView(){
     Double lat = myLocation.getLatitude();
     Double lng = myLocation.getLongitude();
     GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
     mapController.setCenter(point);
}
}
3.) Compile and build the project.
4.) Run on 2.1 simulator which has google api support for the output.
Output

No comments: