Get current latitude and longitude in the string

纵然是瞬间 提交于 2019-11-26 14:51:53

问题


I am trying to get latitude and longitude but sometime network is available but I am not getting value of latitude and longitude. I am using MyLocationListener class and put condition all but some time value is not getting.

protected void showCurrentLocation() 
{
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location != null) 
    {
        counter++;          
        latitude=location.getLatitude();
        longitude=location.getLongitude();
        altitude=location.getAltitude();
    }
 }   

private class MyLocationListener implements LocationListener
{
    @Override
    public void onLocationChanged(Location location)
    {
         counter++;
         latitude=location.getLatitude();
         longitude=location.getLongitude();
         altitude=location.getAltitude();
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle b) 
    {
    }
    @Override
    public void onProviderDisabled(String s) 
    {   
    }
    @Override
    public void onProviderEnabled(String s) 
    {
    }
}

回答1:


Here Best way to get Longitude Latitude:

/** PROCESS for Get Longitude and Latitude **/
locationManager     = (LocationManager) getSystemService(LOCATION_SERVICE);

// Define a listener that responds to location updates

LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.

        longitude = String.valueOf(location.getLongitude());
        latitude = String.valueOf(location.getLatitude());
        Log.d("msg", "changed Loc : "+longitude + ":"+latitude);
    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};

// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

// check if GPS enabled     
if(isGPSEnabled){

    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if(location != null)
    {
        longitude = String.valueOf(location.getLongitude());
        latitude = String.valueOf(location.getLatitude());
        Log.d("msg", "Loc : "+longitude + ":"+latitude);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }else
    {
        /*
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setCostAllowed(true);
        String provider = locationManager.getBestProvider(criteria, true);
         */

        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if(location != null)
        {
            longitude = String.valueOf(location.getLongitude());
            latitude = String.valueOf(location.getLatitude());

            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        }else
        {
            longitude   = "0.00";
            latitude    = "0.00";
        }
    }
}

if device is not able to get currentLocation using GPS then it'll be take fron NetworkProvider or else it'll take 0,0 as my requirement but you can modify as per your requirement

May it'll helpful to you.

Happy Coding :D




回答2:


See, this is a very common problem. In order to have exact Latitude and Longitude ,You must use GPS also it must me activated on the mobile device but GPS too have some limitations. GPS works most efficiently under open sky. you will not have a clear range inside a building. So try your code under open sky and check the response.



来源:https://stackoverflow.com/questions/19175129/get-current-latitude-and-longitude-in-the-string

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