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

…衆ロ難τιáo~ 提交于 2019-12-03 22:07:04

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";
}

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!