简体   繁体   中英

how to get the current location latitude and longitude of android mobile device?

I have implemented sample application for get the current location latitude longitude.If i lunch my application in emulator and i am sending latitude and longitude from Emulator Controls at eclipse then i am getting current location latitude and longitude which from emulator controls.If i lunch the same application in real device then i am not able to get current location latitude and longitude

I have implemented code for get the current location latitude and longitude as follows:

      LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

  LocationListener mlocListener = new MyLocationListener();
  mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 11, 11, mlocListener);


   public class MyLocationListener implements LocationListener
{

  @Override
  public void onLocationChanged(Location loc)
  {

    loc.getLatitude();
    loc.getLongitude();

   Log.v("11111","Latitude :"+loc.getLatitude());
   Log.v("22222","Longitude :"+ loc.getLongitude());

    }

}

From the above code i am not getting current location latitude and longitude in real android device.

How can i get current location latitude and longitude of real device?

please any body help me

GPS_PROVIDER does not work in bound place. So if gps_provider is enabled but you get null location you can replace provider from gps to NETWORK_PROVIDER.

prasad try this code ..by using this i am successfully getting lat long

        private Location location = null;
        private LocationManager locationManager = null;
    locationManager = (LocationManager) context.getSystemService                              (Context.LOCATION_SERVICE);

    Criteria locationCritera = new Criteria();
    locationCritera.setAccuracy(Criteria.ACCURACY_FINE);
    locationCritera.setAltitudeRequired(false);
    locationCritera.setBearingRequired(false);
    locationCritera.setCostAllowed(true);
    locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);

    String providerName = locationManager.getBestProvider(locationCritera,
            true);
    location = locationManager.getLastKnownLocation(providerName);
    locationListener = new MyLocationListener();

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, locationListener);

    currentLocation = context.getSharedPreferences(PREFS_NAME, 0);
    editor = currentLocation.edit();
}
public String getCurrentLatitude() {
    try {
        if (!currentLocation.getString("currentLatitude", "")
                .equalsIgnoreCase(""))
            return currentLocation.getString("currentLatitude", "");
        else if (location.getLatitude() != 0.0)
            return Double.toString(location.getLatitude());
        else
            return "0.0";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "0.0";
}

public String getCurrentLongitude() {
    try {


        if (!currentLocation.getString("currentLongitude", "")
                .equalsIgnoreCase(""))
            return currentLocation.getString("currentLongitude", "");
        else if (location.getLongitude() != 0.0)
            return Double.toString(location.getLongitude());
        else
            return "0.0";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "0.0";
}

are you trying on emulator or device. if you are trying in device then your device should have to be near window or in open place.

override these methods in your MyLocationListener. so that you can track your GPS status. check and see if you can get whats the problem

public void onProviderDisabled(String provider)

        {

            Toast.makeText( getApplicationContext(),"Gps Disabled",

                    Toast.LENGTH_SHORT ).show();

        }

        public void onProviderEnabled(String provider)

        {

            Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();

        }

        public void onStatusChanged(String provider, int status, Bundle extras)

        {

        }

On a real device, you will have to be patient and wait for a fix from the satellites. This can take several minutes, even with a clear view of the sky.

For testing outside, you would be better advised to have a simple text box in your app, initialise it to something like "No GPS fix yet", then send a string comprised of the lat/long coordinates to it when the location changes.

Maybe this is more clear. I will change the conditional to something more efficient later if it is possible

    double longitude = 0;
    double latitude = 0;

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) {
        Location location = lm
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    } else {
        Location location = lm
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    }

There are two types of location providers, GPS Location Provider Network Location Provider

package com.shir60bhushan.gpsLocation;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;

import android.util.Log;

public class MainActivity extends Activity implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
protected String latitude,longitude; 
protected boolean gps_enabled,network_enabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.textview1);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
}

@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}
}

This Link can help you for more information http://androidtutorials60.blogspot.in/2013/09/gps-current-location-of-device.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM