如何让的经度和纬度的android [英] how to get longitude and latitude in android

查看:119
本文介绍了如何让的经度和纬度的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 双纬度= loc.getLatitude(); //导致的结果为空,所以无法知道经度和纬度
双LNG = loc.getLongitude(); 的LocationManager的LocationManager =(的LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    字符串提供商= LocationManager.GPS_PROVIDER;
    地点= locationManager.getLastKnownLocation(供应商); //结果为空!

这是code,以获得GPS状态。它的工作原理罚款:

 公共无效onGpsStatusChanged(INT事件){//获取GPS雕像
                的LocationManager的LocationManager =(的LocationManager)GpsActivity.this.getSystemService(Context.LOCATION_SERVICE);
                GpsStatus状态= locationManager.getGpsStatus(NULL);
                字符串satelliteInfo = updateGpsStatus(事件状态);
                myTextView.setText(satelliteInfo); //做工精细,搜索卫星:16
    }
    };
        私人字符串updateGpsStatus(INT事件,GpsStatus状态){
            StringBuilder的SB2 =新的StringBuilder();
            如果(状态== NULL){
                sb2.append(搜索卫星数字+0);
            }否则如果(事件== GpsStatus.GPS_EVENT_SATELLITE_STATUS){
                INT maxSatellites = status.getMaxSatellites();
                迭代器< GpsSatellite>它= status.getSatellites()迭代器()。
                numSatelliteList.clear();
                诠释计数= 0;
                而(it.hasNext()及和放大器; COUNT< = maxSatellites){
                    GpsSatellite S = it.next();
                    numSatelliteList.add(多个);
                    算上++;
                }
                sb2.append(搜寻卫星编号:+ numSatelliteList.size());
            }
            返回sb2.toString();
        }


解决方案

getLastKnownLocation()只返回最近的GPS定位,如果有的话。您需要实现一个LocationListener的,并使用<一个href=\"http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29\"><$c$c>LocationManager#requestLocationUpdates()获取一个新的位置。


实施基础:

 公共类示例扩展活动实现LocationListener的{
    的LocationManager mLocationManager;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        mLocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);        地点= mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        如果(位置!= NULL){
            //做一些与最近的位置锁定
            //否则等待下面的更新
        }
        其他{
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,这一点);
        }
    }    @覆盖
    公共无效onLocationChanged(地点){
        如果(位置!= NULL){
            Log.v(位置发生改变,location.getLatitude()+和+ location.getLongitude());
        }
    }
    //等。
}

I want to find the longitude and latitude of my current location, but I keep get 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!

This is the code to get the GPS status. It works fine:

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();  
        }

解决方案

getLastKnownLocation() only returns a recent GPS fix, if available. You need to implement a LocationListener and use LocationManager#requestLocationUpdates() to fetch a new location.


Basic implementation:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆