Location User
Location User
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
MapsActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mf = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mf.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap gMap) {
mMap = gMap;
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create()
.setInterval(1000)
.setFastestInterval(1000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
}
}
@Override
public void onLocationChanged(Location location) {
if (mMarker != null) mMarker.remove();
LatLng current = new LatLng(location.getLatitude(),
location.getLongitude());
mMarker = mMap.addMarker(new
MarkerOptions().position(current).title("You"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(current, 13));