user3741413 user3741413 - 2 years ago 119
Java Question

Google Maps API v2 tracking phone

I'm creating an app, and I just got it to show the map on the screen. Now I was wondering how can I have it find the user and track where he/she goes. I did the

, but all that does is show a button, and when you click it nothing happens. I've never used google maps api before so I don't know much about it.

Answer Source

There you have got this trouble good explained -

You have to implement getting user's location out of Google Maps API. You need to use those permissions:

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

There is class example which shows that, what you want. Got this from page I wrote above:

public class MainActivity extends FragmentActivity implements LocationListener {

    GoogleMap googleMap;

    protected void onCreate(Bundle savedInstanceState) {
        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);

        }else { // Google Play Services are available

            // Getting reference to the SupportMapFragment of activity_main.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(;

            // Getting GoogleMap object from the fragment
            googleMap = fm.getMap();

            // Enabling MyLocation Layer of Google Map

            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();

            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);

            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);

            locationManager.requestLocationUpdates(provider, 20000, 0, this);
    public void onLocationChanged(Location location) {

        TextView tvLocation = (TextView) findViewById(;

        // Getting latitude of the current location
        double latitude = location.getLatitude();

        // Getting longitude of the current location
        double longitude = location.getLongitude();

        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        // Showing the current location in Google Map

        // Zoom in the Google Map

        // Setting latitude and longitude in the TextView tv_location
        tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );


    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download