Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mobile Programming Dr. Mohsin Ali Memon.

Similar presentations


Presentation on theme: "Mobile Programming Dr. Mohsin Ali Memon."— Presentation transcript:

1 Mobile Programming Dr. Mohsin Ali Memon

2 Google Maps API V2 The Google Maps Android API allows you to display a Google map in your Android application.  In version 1 of Google Maps for Android, Google used the MapView to display map data. In version 2, the MapView is deprecated; instead, you have to use a MapFragment. In addition, you no longer need to create a project that targets the Google APIs platform, as Google has migrated a lot of services into Google Play services.

3 Step 1 The first step in using Google Maps v2 for Android is to download the Google Play Services SDK. The Google Play Services SDK contains the APIs needed by Google Maps and hence you need to download this and reference it in your Android application. The next step is to import the Google Play Service Library project into your workspace so that you can import it into your Android project. Adding this Library to your current project

4 Step 2 Enabling Google Maps for your Application
Using the Web browser on your computer, go to the Google APIs Console located athttps://code.google.com/apis/console. Go to

5 Step 3: Getting the Maps API Key
In order to use Google Maps v2 for Android, you need to apply for a Maps API key. In order to apply for a Maps API key, you need to pass Google the SHA1 hash of the certificate that you will use for signing your app. C:\Program Files\Java\jdk1.7.0_21\bin> keytool -list -alias androiddebugkey -keystore "<path of debug.keystore>" -storepass android -keypass android –v

6 Generating Keys

7 Configuring Android key for Google Maps API

8 Configuring AndroidManifest file
Back in Eclipse, enter the following code in AndroidManifest.xml. Be sure to take note of the package name of your project as well as put it the Maps API key that you have obtained in the previous step <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="<Your_MAPS_API_KEY>" /> <meta-data android:name="com.google.android.gms.version" />

9 Adding Permissions to the Manifest
<uses-permission android:name="com.example.testgooglemaps.permission.MAPS_RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name= "com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name= "android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

10 The Layout file Replace the current code with
<fragment xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment" />

11 MainActivity import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; public class MainActivity extends FragmentActivity { GoogleMap protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); map = ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); if (map == null) { Toast.makeText(this, "Google Maps not available", Toast.LENGTH_LONG).show(); } } }

12 Adding menu options @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }

13 Main.xml <item android:id="@+id/menu_showcurrentlocation"
android:orderInCategory="100" android:showAsAction="never" android:title="Show Current Location"/> android:title="Line connecting 2 points"/> </menu> <menu xmlns:android=" m/apk/res/android" > <<item android:orderInCategory="100" android:showAsAction="never" android:title="Zoom In"/> <item android:title="Zoom Out"/> <item android:orderInCategory="100" android:showAsAction="never" android:title="Go to a location"/> android:title="Add a marker"/> android:title="Get Current Location"/>

14 Main Activity cont’d case R.id.menu_gotolocation:
CameraPosition cameraPosition = new CameraPosition.Builder() .target(MEHRAN_UET) // Sets the center of the map to // Mehran UET Jamshoro .zoom(17) // Sets the zoom .bearing(90) // Sets the orientation of the camera to east .tilt(30) // Sets the tilt of the camera to 30 degrees .build(); // Creates a CameraPosition from the builder map.animateCamera(CameraUpdateFactory.newCameraPosition( cameraPosition)); break; case R.id.menu_getcurrentlocation: // ---get your current location and display a blue dot--- map.setMyLocationEnabled(true); private static final LatLng MEHRAN_UET = new LatLng( , ); private static final LatLng NIAZ_STADIUM = new LatLng( , ); public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_zoomin: map.animateCamera(CameraUpdateFactory.zoomIn()); break; case R.id.menu_zoomout: map.animateCamera(CameraUpdateFactory.zoomOut());

15 Main Activity cont’d case R.id.menu_lineconnecttwopoints:
case R.id.menu_showcurrentlocation: Location myLocation = map.getMyLocation(); LatLng myLatLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude()); CameraPosition myPosition = new CameraPosition.Builder() .target(myLatLng).zoom(17).bearing(90).tilt(30).build(); map.animateCamera( CameraUpdateFactory.newCameraPosition(myPosition)); break; case R.id.menu_lineconnecttwopoints: //---add a marker at Niaz Stadium--- map.addMarker(new MarkerOptions() .position(NIAZ_STADIUM) .title(“niaz") .snippet(“stadium") .icon(BitmapDescriptorFactory.defaultMarker( BitmapDescriptorFactory.HUE_AZURE))); //---draw a line connecting MUET and Niaz Dtadium map.addPolyline(new PolylineOptions() .add(MEHRAN_UET, NIAZ_STADIUM).width(5).color(Color.RED)); break; } return true;}

16

17


Download ppt "Mobile Programming Dr. Mohsin Ali Memon."

Similar presentations


Ads by Google