如何在Android中获取经度和纬度

Posted

技术标签:

【中文标题】如何在Android中获取经度和纬度【英文标题】:How to get longitude and latitude in Android 【发布时间】:2012-11-02 13:01:01 【问题描述】:

我想查找我当前位置的经纬度,但我一直得到NULL

double lat = loc.getLatitude(); //Cause the result is null, so can't know longitude and latitude
double lng = loc.getLongitude();


 LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);  
    String provider = LocationManager.GPS_PROVIDER;  
    Location location = locationManager.getLastKnownLocation(provider); //result is null!

这是获取 GPS 状态的代码。它工作正常:

public void onGpsStatusChanged(int event)  // get the GPS statue  
                LocationManager locationManager = (LocationManager) GpsActivity.this.getSystemService(Context.LOCATION_SERVICE);  
                GpsStatus status = locationManager.getGpsStatus(null);  
                String satelliteInfo = updateGpsStatus(event, status);
                myTextView.setText(satelliteInfo);//work fine ,searched satellite:16
    
    ;
        private String updateGpsStatus(int event, GpsStatus status)   
            StringBuilder sb2 = new StringBuilder("");  
            if (status == null)   
                sb2.append("searched satellite number" +0);  
             else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS)   
                int maxSatellites = status.getMaxSatellites();  
                Iterator<GpsSatellite> it = status.getSatellites().iterator();  
                numSatelliteList.clear();  
                int count = 0;  
                while (it.hasNext() && count <= maxSatellites)   
                    GpsSatellite s = it.next();  
                    numSatelliteList.add(s);  
                    count++;  
                  
                sb2.append("searched satellite number:" + numSatelliteList.size());  
            
            return sb2.toString();  
        

【问题讨论】:

复制:***.com/questions/2227292/… 我搜索了很多教程,但是[我找到了当前位置的最佳链接][1] [1]:***.com/a/17857993/1318946 【参考方案1】:

getLastKnownLocation() 仅返回最近的 GPS 定位(如果可用)。您需要实现 LocationListener 并使用 LocationManager#requestLocationUpdates() 获取新位置。


基本实现:

public class Example extends Activity implements LocationListener 
    LocationManager mLocationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null) 
            // Do something with the recent location fix
            //  otherwise wait for the update below
        
        else 
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        
    

    @Override
    public void onLocationChanged(Location location) 
        if (location != null) 
            Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
        
    
    // etc..

【讨论】:

以上是关于如何在Android中获取经度和纬度的主要内容,如果未能解决你的问题,请参考以下文章

如何在Android中获取经度和纬度

如何在Android中获取某人的纬度和经度?

如果我们在 Android 中使用位置 API 给出纬度和经度,如何获取地址

如何在没有互联网连接的情况下在android中获取当前纬度和经度?

如何使用 Here Map for android SDK 获取纬度和经度?

Android:如何获取经度和纬度的当前位置?