Lam Viec Voi Google Map Trong Android

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Lm vic vi Google Maps trong Android

An article by admin 2 Comments Google cung cp cho chng ta mt cch d dng tch hp Google Maps vo cc ng dng Android. Hm nay chng ta s s tm hiu lm th no nhng Google Maps vo ng dng android ca bn.

Ti v ton b code Lm Vic Vi Google Maps Trong Android


DownloadLogin Required

To d n mi bng cch chn Google API SDK


Khi bn to mi d n, bn cn phi chn target SDK l Google API. Nu bn khng c Google API SDK hy ti v bng cch m SDK Manager ca bn 1. To mt d n mi bng cch vo File New Android Project. in vo tt c cc chi tit, chn Google API l target sdk v t tn cho activity ca bn.

2. Th vin Google Map khng phi l mt phn ca th vin Android, chng ta cn phi khai bo th vin trong tp tin AndroidManifest.xml. Ngoi ra chng ta cn phi cho php kt ni internet. M tp tin AndroidManifest.xml ca bn v sa i nh sau. 01 <?xml version="1.0" encoding="utf-8"?> 02 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 03 04 05 06 07 08 09 package="com.androidhive.googlemaps" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 </manifest> <!-- Allow to connect with internet --> <uses-permission android:name="android.permission.INTERNET" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <activity android:label="@string/app_name" android:name=".AndroidGoogleMapsActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <!-Add Google Map Library --> <uses-library android:name="com.google.android.maps" />

Ly Google Map Key


s dng Google Maps bn cn phi ly c kha. Ti gi s bn ang lm vic trn Windows PC. By gi bn cn c c kha MD5 t ci t jdk ca bn 3. M du nhc lnh bng cch g cmd trong Run. Start Run g cmd. c:\<path-to-jdk-dir>\bin\keytool.exe -list -alias androiddebugkey -keystore 1 "C:\users\<user-name>\.android\debug.keystore" -storepass android -keypass android di y l lm th no c fingerprint MD5 1 keytool.exe -list -alias androiddebugkey -keystore "C:\users\ravi\.android\debug.keystore" -storepass android -keypass android

4. By gi bn cn ly kha Google Maps bng cch s dng fingerprint MD5. Truy cp Android Maps API v ly kha da vo fingerprint MD5 ca bn.

5. By gi m tp tin main.xml trong src layout main.xml v chn m sau y. ng qun t kha ca bn. 1 <?xml version="1.0" encoding="utf-8"?> 2 <com.google.android.maps.MapView 3 4 5 6 7 8 9 /> 6. By gi m lp MainActivity ca bn v m rng n t lp MapActivity. 1 public class AndroidGoogleMapsActivity extends MapActivity { xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="05M-7qOGbEjYduPPUdQgJt9ysL8HToawGdvu_ow"

7. Sau khi m rng Activity ca bn t MapActivity bn cn override isRouteDisplayed() v cng c th t ch xem mc nh ca bn vo main.xml 01 package com.androidhive.googlemaps; 02 03 import android.os.Bundle; 04 import com.google.android.maps.MapActivity; 05 06 public class AndroidGoogleMapsActivity extends MapActivity { 07 08 09 10 11 12 13 14 15 16 17 } 8. By gi chy d n ca bn, bn s thy bn hin th trn thit b ca bn. Nu khng hy lm li tt c cc bc trn. } @Override protected boolean isRouteDisplayed() { return false; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

Hin th Controls Zoom


Cho n by gi chng ta c mt bn tnh ch vi chc nng ko v cc tnh nng di chuyn. Chng ta c th thm iu khin zoom phng to hoc thu nh bn . Bn ca Google sn c iu khin phng to thu nh, do , tt c chng ta cn lm l g vi dng. 01 package com.androidhive.googlemaps; 02 03 import android.os.Bundle; 04 05 import com.google.android.maps.MapActivity; 06 import com.google.android.maps.MapView; 07 08 public class AndroidGoogleMapsActivity extends MapActivity { 09 10 11 12 13 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

14 15 16 17 18 19 20 21 22 23 24 } } }

// Displaying Zooming controls MapView mapView = (MapView) findViewById(R.id.mapView); mapView.setBuiltInZoomControls(true);

@Override protected boolean isRouteDisplayed() { return false;

Thay i Loi hin th ca Bn


Bn cng c th thay i loi bn nh v tinh, StreetView, 1 mapView.setSatellite(true); // Satellite View 2 mapView.setStreetView(true); // Street View 3 mapView.setTraffic(true); // Traffic View

Hin th V tr bng cch truyn Latitude v Longitude


M di y s hin th mt v tr trn bn bng truyn vo v v kinh ca v tr . 1 MapController mc = mapView.getController(); 2 double lat = Double.parseDouble("48.85827758964043"); // latitude 3 double lon = Double.parseDouble("2.294543981552124"); // longitude 4 GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6)); 5 mc.animateTo(geoPoint); 6 mc.setZoom(15); 7 mapView.invalidate();

Thm im nh du trn bn
hin th im nh du trn bn , bn cn to ra lp mi m rng t ItemizedOverlay. To lp mi v t tn l nh AddItemizedOverlay.java v vit code sau y. 01 package com.androidhive.googlemaps; 02 03 import java.util.ArrayList; 04 05 import android.content.Context; 06 import android.graphics.drawable.Drawable; 07 import android.util.Log; 08 09 import com.google.android.maps.ItemizedOverlay; 10 import com.google.android.maps.OverlayItem; 11 12 public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> { 13 14 private ArrayList<OverlayItem> mapOverlays

= new ArrayList<OverlayItem>(); 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 } public void addOverlay(OverlayItem overlay) { mapOverlays.add(overlay); this.populate(); } } @Override protected boolean onTap(int index) { Log.e("Tap", "Tap Performed"); return true; } @Override public int size() { return mapOverlays.size(); } @Override protected OverlayItem createItem(int i) { return mapOverlays.get(i); } public AddItemizedOverlay(Drawable defaultMarker, Context context) { this(defaultMarker); this.context = context; public AddItemizedOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); } private Context context;

By gi m MainActivity.java (trong trng hp ny l AndroidGoogleMapsActivity.java) v vit code sau y. 1 List<Overlay> mapOverlays = mapView.getOverlays();

2 Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red); 3 AddItemizedOverlay itemizedOverlay = 4 new AddItemizedOverlay(drawable, this); 5 6 OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item"); 7 8 itemizedOverlay.addOverlay(overlayitem); 9 mapOverlays.add(itemizedOverlay);

Ly Latitude v Longitude ca v tr chm(tr)


Bn cng c th c c cc Latitude v Longitude ca v tr c xc ng. M AddItemizedOverlay.java ca bn v thm phng thc sau y. 01 package com.androidhive.googlemaps; 02 03 import java.util.ArrayList; 04 05 import android.content.Context;

06 import android.graphics.drawable.Drawable; 07 import android.util.Log; 08 import android.view.MotionEvent; 09 import android.widget.Toast; 10 11 import com.google.android.maps.GeoPoint; 12 import com.google.android.maps.ItemizedOverlay; 13 import com.google.android.maps.MapView; 14 import com.google.android.maps.OverlayItem; 15 16 public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> { 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 if (event.getAction() == 1) { GeoPoint geopoint = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); // latitude double lat = geopoint.getLatitudeE6() / 1E6; // longitude double lon = geopoint.getLongitudeE6() / 1E6; /*................. Add this method ........*/ @Override public boolean onTouchEvent(MotionEvent event, MapView mapView) {

Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show(); 32 } 33 34 35 36 } } return false;

By gi chy d n ca bn v chm vo a im c th c c Latitude v Longitude ca a im .

You might also like