emen emen - 1 year ago 79
Android Question

How to get user location in android google map

i have following code for show user location in my app :

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, LocationListener {
private GoogleMap googleMap;
private Marker userMarker;
private Marker destinationMarker;
private Location userLocation;
private Location destinationLocation;
private static final int REQUEST_CODE = 101;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);

}

private void setupLocationManager() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION
, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE);
}
return;
}
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
onLocationChanged(location);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 5, this);
}
}
}

@Override
public void onMapReady(final GoogleMap googleMap) {
this.googleMap = googleMap;
googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
destinationLocation = new Location(LocationManager.GPS_PROVIDER);
destinationLocation.setLatitude(latLng.latitude);
destinationLocation.setLongitude(latLng.longitude);
Polyline line = googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(userLocation.getLatitude(),
userLocation.getLongitude()), new LatLng(destinationLocation.getLatitude(),
destinationLocation.getLongitude())).color(Color.BLUE).width(10).geodesic(true));
destinationMarker = googleMap.addMarker(new MarkerOptions().position(latLng).title("Destination Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_destination_marker)));
}
});
setupLocationManager();
}

@Override
public void onLocationChanged(Location location) {
if (location != null && googleMap != null) {
userLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
if (userMarker != null) {
userMarker.remove();
}
userMarker = googleMap.addMarker(new MarkerOptions().position(latLng).title("Your Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_user_marker)));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
googleMap.animateCamera(cameraUpdate);
}else {
Toast.makeText(MainActivity.this,"Location is null",Toast.LENGTH_LONG).show();
}
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE) {
if (grantResults[0] == 0) {
setupLocationManager();
}
}
}
}


My manifest permissions :

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />


I have two problems:


  • first : my code don't show user location in real device(app always
    show Location is null(toast message in onLocationChanged()) I
    dont know why (solved)

  • second : i want to my Polyline shows in roads. I know about
    isGeodesic()
    function but it doesn't show when i use...


Answer Source

use this tutorial for your second prob and add your location to this tutorial code like your codes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download