Praktikum X Android Location API Google Map Pokok Bahasan: • Membuat Aplikasi Google Map Tujuan Belajar:
Setelah mempelajari modul ini mahasiswa diharapkan mengetahui: • •
Cara membuat aplikasi Google Map Mengenal Koordinat pemetaan (Longitude dan Latitude)
Teori Dasar Salah satu ciri mobile phone /tablet pc adalah portabilitas, sehingga tidak mengherankan bahwa beberapa fitur android sangat menarik seperti layanan yang memungkinkan anda menemukan, mencari serta menvisualisasikan posisi kita ke dalam peta lokasi fisik (google maps). Percobaan 1: 1. Buat project baru 2. Tambahkan coding berikut pada main.xml
3. Tambahkan coding dibawah pada file activity: package com.locationapi; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; import android.app.Activity; import android.content.Context; public class LokasiActivity extends Activity implements LocationListener { private TextView latituteField; private TextView longitudeField; private LocationManager locationManager; private String provider; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); latituteField = (TextView) findViewById(R.id.txtLat);
longitudeField = (TextView) findViewById(R.id.txtLong); locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(provider); if (location != null) { System.out.println("Provider " + provider + " has been selected."); onLocationChanged(location); } else { latituteField.setText("Lokasi tidak tersedia"); longitudeField.setText("Lokasi tidak tersedia"); } } protected void onResume() { super.onResume(); locationManager.requestLocationUpdates(provider, 400, 1, this); } protected void onPause() { super.onPause(); locationManager.removeUpdates(this); } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub float lat = (float) (location.getLatitude()); float lng = (float) (location.getLongitude()); latituteField.setText(String.valueOf(lat)); longitudeField.setText(String.valueOf(lng)); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub Toast.makeText(this, "Disabled provider " + provider, Toast.LENGTH_SHORT).show(); }
@Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub Toast.makeText(this, "Enabled new provider " + provider, Toast.LENGTH_SHORT).show(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } 4. Tambahkan 3 uses-permission berikut pada AndroidManifest.xml: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.locationapi" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
5. Pilih DDMS > Emulator Control > ubah nilai Longitude dan Latitude > klik Send > amati perubahan yang terjadi pada emulator.
Percobaan 2:
Persiapan Device 1.
Install Google Play Service pada Android SDK manager
2. Import project yang berada dalam …sdk\extras\google\google_play_services\libproject Dengan cara File > Import > adroid > Existing android project into workspace > browse > pilih folder libproject tersebut. 3. Buka command prompt dan ketik: keytool -list -v -alias androiddebugkey -keystore C:\Users\
\.android\debug.keystore storepass android -keypass android
4. Buka https://code.google.com/apis/console/, pilih services dan aktifkan Google Maps Android API v2.
5. Pilih API Access > Create new Android key
6. Isi kode SHA1 yang terdapat pada cmd beserta nama package project yang akan dibuat.
7. Jalankan Android Virtual Device dan install google play service melalui cmd setelah AVD selesai booting:
Pembuatan Program 1. Buat android project baru:
2. Tambahkan Project library yang telah didownload.
3. Activity_Main.xml
<Button android:id="@+id/btnUnsada" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/map" android:layout_alignLeft="@+id/map" android:onClick="onClick_Unsada" android:text="Unsada" /> <Button android:id="@+id/btnRsj" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="14dp" android:onClick="onClick_Rsj" android:text="RSJ" /> <Button android:id="@+id/btnRagunan" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_above="@+id/map" android:layout_alignRight="@+id/map" android:onClick="onClick_Ragunan" android:text="Ragunan" /> 4. AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mapapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <uses-permission android:name="com.mapapp.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"/> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyAd7Pygd2Dhw19VgbfJyjk6jgurAEJsH60"/>
5. MapActivity.java package com.mapapp; import import import import
android.os.Bundle; android.app.Activity; android.view.Menu; android.view.View;
import import import import import import
com.google.android.gms.maps.CameraUpdate; com.google.android.gms.maps.CameraUpdateFactory; com.google.android.gms.maps.GoogleMap; com.google.android.gms.maps.MapFragment; com.google.android.gms.maps.model.LatLng; com.google.android.gms.maps.model.MarkerOptions;
public class MapActivity extends Activity { private final LatLng LOCATION_UNSADA = new LatLng(-6.229671, 106.924331); //lokasi unsada private final LatLng LOCATION_RSJ = new LatLng(-6.168033, 106.798181); //lokasi rsj private final LatLng LOCATION_RAGUNAN = new LatLng(-6.309532, 106.820370); //lokasi ragunan private GoogleMap map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); map.addMarker(new MarkerOptions().position(LOCATION_UNSADA).title("Saya kuliah disini!"));
map.addMarker(new MarkerOptions().position(LOCATION_RSJ).title("Saya berobat kesini!")); map.addMarker(new MarkerOptions().position(LOCATION_RAGUNAN).title("Saya tinggal disini!")); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.map, menu); return true; }
public void onClick_Unsada(View v) { map.setMapType(GoogleMap.MAP_TYPE_TERRAIN); CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_UNSADA, 14); map.animateCamera(update); } public void onClick_Rsj(View v) { map.setMapType(GoogleMap.MAP_TYPE_NORMAL); CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_RSJ, 16); map.animateCamera(update); } public void onClick_Ragunan(View v) { map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_RAGUNAN, 15); map.animateCamera(update); } }
6. Jalankan Aplikasi
Bila masih bingung bisa hubungi : [email protected]