0% found this document useful (0 votes)
8 views2 pages

Location User

The document outlines an Android application that utilizes Google Maps and location services. It includes permissions for internet and location access, a layout for displaying a map, and a MapsActivity class that manages the map and updates the user's location. The application sets up a GoogleApiClient to request location updates and display the user's current position on the map.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Location User

The document outlines an Android application that utilizes Google Maps and location services. It includes permissions for internet and location access, a layout for displaying a map, and a MapsActivity class that manages the map and updates the user's location. The application sets up a GoogleApiClient to request location updates and display the user's current position on the map.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

User's Location and Car

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

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,


LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {

private GoogleMap mMap;


private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private Marker mMarker;

@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);
}

private synchronized void buildGoogleApiClient() {


mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
mGoogleApiClient.connect();
}

@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));

@Override public void onConnectionSuspended(int i) {}


@Override public void onConnectionFailed(ConnectionResult connectionResult) {}
}

You might also like