0

I am trying to implement the users location in my android project. I have tried alot on many of the codes found in the internet but nothing worked. Neither the Location Manager nor the Location client also tried the new one GoogleApiClient too. But i don't know where is my mistake. Do we require any of the Api's key to get users location? I have kept my codes below: Location_Finder2.java

package com.notification.messaging;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

public class Location_Finder2 extends Activity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener{
    LocationRequest locationRequest;
    LocationClient locationClient;
    Location location;
    Double longitude = 0.0d;
    Double latitude = 0.0d;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationClient = new LocationClient(this, this, this);
        locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(5000);
        locationRequest.setFastestInterval(1000);
    }
    @Override
    public void onLocationChanged(Location arg0) {
        locationClient.removeLocationUpdates(this);
        longitude = location.getLongitude();
        latitude = location.getLatitude();
        Log.i("New Latitude && Longitude:", "Longitude:" + longitude + ", Latitude:" + latitude);
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnected(Bundle arg0) {
        Log.i("Location_2","dhsjchdjcjdjcjkdjcjdn");
        location = locationClient.getLastLocation();
        if (location == null){
            locationClient.requestLocationUpdates(locationRequest, this);
        }
        else {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            Log.i("Latitude && Longitude:", "Longitude:" + longitude + ", Latitude:" + latitude);
        }
    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub

    }

}

I am not getting any location update waiting for a long time but i don't find what my mistake is.

PaladiN
  • 4,625
  • 8
  • 41
  • 66
  • http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ take a look at it. It's 100% working.. – Nabin Aug 23 '14 at 13:52
  • be sure to give internet permission in android manifest.xml file – Nabin Aug 23 '14 at 13:53
  • @Nabin this one is using location manager and it is not perfect for giving the max accurate location which i am in need of. Since the link below says something like that http://stackoverflow.com/questions/20908822/android-difference-between-locationmanager-addproximityalert-locationclien – PaladiN Aug 23 '14 at 14:41

2 Answers2

0

First add this permission in Manifest -

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

Then add this code in New class

private class MyLocationListener implements LocationListener {
//Implement Location Listener

double latitude, longitude;

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Create instance

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

longitude = location.getLongitude();
latitude = location.getLatitude();
//Implement LocationListener and get coordinates

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
        }
    }
}

You may also want to add the ACCESS_COARSE_LOCATION permission for when GPS isn't available and select your location provider with the getBestProvider() method .

You may also like to check whether GPS is enabled or not -

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
    buildAlertMessageNoGps();
}

private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
       .setCancelable(false)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
               startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                dialog.cancel();
           }
       });
final AlertDialog alert = builder.create();
alert.show();
}
Confuse
  • 5,646
  • 7
  • 36
  • 58
0

On the first look it seems that you never connect the LocationClient. If you don't get the Logcat message Log.i("Location_2","dhsjchdjcjdjcjkdjcjdn"); it's a good indicator that you have to call locationClient.connect(); in your onCreate().

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    locationClient = new LocationClient(this, this, this);
    locationRequest = new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(5000);
    locationRequest.setFastestInterval(1000);

    locationClient.connect();
}

The rest of the code looks ok to get location updates.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • Sorry to bother you about that as i have forgot about that and have solved the problems. Thanks for this. – PaladiN Aug 23 '14 at 16:48