Kode Mobile Pro

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MainActivity.

java

Java

package com.example.fawfectapp;

import android.Manifest;
import android.content.pm.PackageManager; import
android.graphics.Bitmap; import
android.graphics.BitmapFactory; import
android.location.Location; import
android.os.Bundle; import
android.support.v4.app.ActivityCompat; import
android.support.v4.content.ContextCompat;
import
android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.google.android.gms.common.api.Api; import


com.google.android.gms.common.api.GoogleApiClient; 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.OnMapReadyCallback; 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;
public class MainActivity extends AppCompatActivity
implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
{
private static final int MY_PERMISSIONS_REQUEST_LOCATION = 1;
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check if the device has location permission
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// Request location permission
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
} else {
// Build the GoogleApiClient
buildGoogleApiClient();
}
}

@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}

@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[]
permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled
if (grantResults.length <= 0 || grantResults[0] !=
PackageManager.PERMISSION_GRANTED) {
Log.d("MainActivity", "Permission denied");
} else {
// Permission granted
buildGoogleApiClient();
}
}
}
}

/**
* Builds the GoogleApiClient
*/ private void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Api.Class.SIGN_IN_API)
.addApi(Api.Class.LOCATION_SERVICES_API)
.addOnConnectionFailedListener(this)
.build();
}
@Override
public void onConnected(Bundle connectionHint) {
// Get the user's last location
mLastLocation =
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
// Check if the location is available
if (mLastLocation != null) {
// Add a marker to the map at the user's location
LatLng userLocation = new
LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
mMap.addMarker(new
MarkerOptions().position(userLocation).title("Your Location"));

// Move the camera to the user's location


mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 16));
}
}
@Override
public void onConnectionSuspended(int cause) {
Log.d("MainActivity", "Connection suspended");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult)
{
Log.d("MainActivity", "Connection failed");
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap =

You might also like