Reputation: 361
I am new to google maps API so i am wondering how do I get google maps to detect my current location when it is first loaded. Currently it can only display a marker of a hardcoded location but I want it to be my current location.
This is my current hardcoded location code:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap
LatLng test = new LatLng(33.8675,151.2070);
mMap.addMarker(new MarkerOptions().position(test).title("test"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(test));
}
Upvotes: 0
Views: 9572
Reputation: 1
public void onMapReady(GoogleMap googleMap) {
gMap = googleMap;
if (!gMap.isMyLocationEnabled())
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.INTERNET}, 1);
Toast.makeText(this, "Grant", Toast.LENGTH_LONG).show();
return;
}else {
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
float la = (float) location.getLatitude() ;
float lo = (float) location.getLongitude();
Toast.makeText(this, la+"______________"+lo, Toast.LENGTH_LONG).show();
LatLng TutorialsPoint = new LatLng(la, lo);
gMap.addMarker(new
MarkerOptions().position(TutorialsPoint).title("Tutorialspoint.com"));
gMap.moveCamera(CameraUpdateFactory.newLatLng(TutorialsPoint));
}
}
Upvotes: -1
Reputation: 13494
You can follow this tutorial. You need to use the LocationManager
to get the best provider and use it to get the location.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//show error dialog if GoolglePlayServices not available
if (!isGooglePlayServicesAvailable()) {
finish();
}
setContentView(R.layout.activity_my);
SupportMapFragment supportMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
googleMap = supportMapFragment.getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}
These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent
when the device enters the proximity of a given geographical location.
Check these related links:
Upvotes: 2
Reputation: 277
add this on onActivityResult
LatLng latLng = new LatLng(place.getLatLng().latitude,place.getLatLng().longitude);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
and add this to onMapReady
googleMap.setOnMyLocationChangeListener(myLocationChangeListener);
and add 1 method
private OnMyLocationChangeListener myLocationChangeListener = new OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
if(awal) {
awal=false;
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
}
};
Upvotes: 0