Created
March 9, 2020 09:49
-
-
Save rohangho/c9d85fa92137aa72a62bb6c4321acaa3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.my_map; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import androidx.core.app.ActivityCompat; | |
import androidx.fragment.app.FragmentActivity; | |
import android.Manifest; | |
import android.content.pm.PackageManager; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
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.OnMapReadyCallback; | |
import com.google.android.gms.maps.SupportMapFragment; | |
import com.google.android.gms.maps.model.LatLng; | |
import com.google.android.gms.maps.model.MarkerOptions; | |
import static android.Manifest.permission.ACCESS_FINE_LOCATION; | |
public class DriverMapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener | |
{ | |
private GoogleMap mMap; | |
GoogleApiClient mGoogleApiClients; | |
Location mLastLocation; | |
LocationRequest mLocationRequest; | |
SupportMapFragment mapFragment; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_driver_map); | |
mapFragment = (SupportMapFragment) getSupportFragmentManager() | |
.findFragmentById(R.id.map); | |
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) | |
ActivityCompat.requestPermissions(DriverMapActivity.this,new String[]{ACCESS_FINE_LOCATION},Location_Request_Code); | |
else | |
mapFragment.getMapAsync(this); | |
// Obtain the SupportMapFragment and get notified when the map is ready to be used. | |
} | |
/** | |
* Manipulates the map once available. | |
* This callback is triggered when the map is ready to be used. | |
* This is where we can add markers or lines, add listeners or move the camera. In this case, | |
* we just add a marker near Sydney, Australia. | |
* If Google Play services is not installed on the device, the user will be prompted to install | |
* it inside the SupportMapFragment. This method will only be triggered once the user has | |
* installed Google Play services and returned to the app. | |
*/ | |
@Override | |
public void onMapReady(GoogleMap googleMap) { | |
mMap = googleMap; | |
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,android.Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) | |
return; | |
buildGoogleApiClient(); | |
mMap.setMyLocationEnabled(true); | |
} | |
protected synchronized void buildGoogleApiClient() { | |
mGoogleApiClients=new GoogleApiClient.Builder(this) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.addApi(LocationServices.API) | |
.build(); | |
mGoogleApiClients.connect(); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
mLastLocation=location; | |
LatLng latLng=new LatLng(location.getLatitude(),location.getLongitude()); | |
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); | |
mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); | |
} | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
mLocationRequest=new LocationRequest(); | |
mLocationRequest.setInterval(1000); | |
mLocationRequest.setFastestInterval(1000); | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClients,mLocationRequest,this); | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
} | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
} | |
final int Location_Request_Code=1; | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
switch (requestCode) | |
{ | |
case Location_Request_Code: | |
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED) | |
mapFragment.getMapAsync(this); | |
else | |
Toast.makeText(getApplicationContext(),"please grant access",Toast.LENGTH_LONG).show(); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment