我有一个简单的类GPSListener,它获取GPS坐标:
public class GPSListener implements LocationListener { public static double latitude; public static double longitude; @OverrIDe public voID onLocationChanged(Location loc) { loc.getLatitude(); loc.getLongitude(); latitude = loc.getLatitude(); longitude = loc.getLongitude(); Log.d("GPSListENER ", "lat: "+latitude+" long:"+longitude);} ...
然后尝试在活动中使用该类,只需在活动中的onCreate()函数中调用该类:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new GPSListener(); Criteria criteria = new Criteria(); String bestProvIDer = mlocManager.getBestProvIDer(criteria, false); mlocManager.requestLocationUpdates(bestProvIDer, 0, 0, mlocListener); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); if (mlocManager.isProvIDerEnabled(LocationManager.GPS_PROVIDER)) { lat = GPSListener.latitude; lon = GPSListener.longitude; Log.d("GPS", "lat: "+lat+" long:"+lon); } else { // Todo: GPS not enabled Log.d("GPSERROR", "GPS not enabled"); }
但是,每当我运行该应用程序时,活动中的纬度和经度始终为零.我不太确定如何解决这个问题.
记录时:
Log.d("GPSListENER ", "lat: "+latitude+" long:"+longitude);
它会返回正确的纬度和经度,活动开始后只需一两秒钟.
Log.d("GPSSUCCESS", "lat: "+lat+" long:"+lon);
两者都返回0.0.我的印象是,在执行if语句之前,.requestLocationUpdates会将值传递给lat和lon.我该怎么做?
解决方法:
您正在将公共静态字段用于纬度和经度.
请将其更改为非静态,并使用带有即时对象的setter,getter:
lat = mlocListener.getLatitude();lon = mlocListener.getLongitude();
总结 以上是内存溢出为你收集整理的Android定位服务未更新requestLocationUpdates全部内容,希望文章能够帮你解决Android定位服务未更新requestLocationUpdates所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)