Hi Sobat TekMagz, kali ini saya akan berbagi bagaimana caranya membuat aplikasi sederhana untuk melacak keberadaan kita menggunakan Android Studio.
Buat yang belum mengetahui apa itu android studio, android studio adalah sebuah IDE untuk pengembangan aplikasi dengan platform Android. Sama seperti kombinasi antara Eclipse dan Android Development Tools (ADT). Android Studio sendiri bisa sobat download secara gratis di sini.
Di aplikasi yang akan kita buat ini, akan menggunakan Android Location API dan juga Google Map Services. Android Location API akan secara terus menerus mengambil lokasi device dan akan memperlihatkan posisi long-latnya melalui Google Maps.
Sebelum beranjak ke tutorialnya, ada baiknya sobat semua men generate Google Map API melalui Google Developer Console di sini. Silahkan login menggunakan akun Google sobat semua.
Setelah mendapatkan Google Map API, tambahkan pula Google Play Services ke project dependency, tambahkan di Gradle dependencies di Android Studio.
Berikut adalah code yang perlu sobat masukan di
Android Manifest (AndroidManifest.xml):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.testing.android.lacaklokasi" > <permission android:name="com.testing.android.lacaklokasi.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.testing.android.lacaklokasi.permission.MAPS_RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <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_FINE_LOCATION" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.testing.android.lacaklokasi.LocationActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="isi dengan google map api" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application> </manifest>
Gradle Dependencies untuk Google Maps
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.android.gms:play-services:6.5.87' compile 'com.google.maps.android:android-maps-utils:0.3+' compile 'com.google.android.gms:play-services:6.5.87' compile 'com.android.support:appcompat-v7:21.0.3' }
Layout Lacak Lokasi
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".TaskRoadMap" > <fragment android:id="@+id/map" android:layout_width="wrap_content" android:layout_height="wrap_content" class="com.google.android.gms.maps.SupportMapFragment"/> </RelativeLayout>
Lacak Lokasi Activity
package com.testing.android.lacaklokasi; import android.location.Location; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.widget.Button; import android.widget.TextView; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.PendingResult; import com.google.android.gms.common.api.Status; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.maps.android.ui.IconGenerator; import java.text.DateFormat; import java.util.Date; public class LocationActivity extends FragmentActivity implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private static final String TAG = "LocationActivity"; private static final long INTERVAL = 1000 * 60 * 1; //1 minute private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute Button btnFusedLocation; TextView tvLocation; LocationRequest mLocationRequest; GoogleApiClient mGoogleApiClient; Location mCurrentLocation; String mLastUpdateTime; GoogleMap googleMap; protected void createLocationRequest() { mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(INTERVAL); mLocationRequest.setFastestInterval(FASTEST_INTERVAL); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate ..............................."); //show error dialog if GoolglePlayServices not available if (!isGooglePlayServicesAvailable()) { finish(); } createLocationRequest(); mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); setContentView(R.layout.activity_location_google_map); SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); googleMap = fm.getMap(); googleMap.getUiSettings().setZoomControlsEnabled(true); } @Override public void onStart() { super.onStart(); Log.d(TAG, "onStart fired .............."); mGoogleApiClient.connect(); } @Override public void onStop() { super.onStop(); Log.d(TAG, "onStop fired .............."); mGoogleApiClient.disconnect(); Log.d(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected()); } private boolean isGooglePlayServicesAvailable() { int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (ConnectionResult.SUCCESS == status) { return true; } else { GooglePlayServicesUtil.getErrorDialog(status, this, 0).show(); return false; } } @Override public void onConnected(Bundle bundle) { Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected()); startLocationUpdates(); } protected void startLocationUpdates() { PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this); Log.d(TAG, "Location update started ..............: "); } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { Log.d(TAG, "Connection failed: " + connectionResult.toString()); } @Override public void onLocationChanged(Location location) { Log.d(TAG, "Firing onLocationChanged.............................................."); mCurrentLocation = location; mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); addMarker(); } private void addMarker() { MarkerOptions options = new MarkerOptions(); // following four lines requires 'Google Maps Android API Utility Library' // https://developers.google.com/maps/documentation/android/utility/ // I have used this to display the time as title for location markers // you can safely comment the following four lines but for this info IconGenerator iconFactory = new IconGenerator(this); iconFactory.setStyle(IconGenerator.STYLE_PURPLE); options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime))); options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV()); LatLng currentLatLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()); options.position(currentLatLng); Marker mapMarker = googleMap.addMarker(options); long atTime = mCurrentLocation.getTime(); mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime)); mapMarker.setTitle(mLastUpdateTime); Log.d(TAG, "Marker added............................."); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 13)); Log.d(TAG, "Zoom done............................."); } @Override protected void onPause() { super.onPause(); stopLocationUpdates(); } protected void stopLocationUpdates() { LocationServices.FusedLocationApi.removeLocationUpdates( mGoogleApiClient, this); Log.d(TAG, "Location update stopped ......................."); } @Override public void onResume() { super.onResume(); if (mGoogleApiClient.isConnected()) { startLocationUpdates(); Log.d(TAG, "Location update resumed ....................."); } } }