android – 在地图中显示当前位置时获取纬度和经度0.0

android – 在地图中显示当前位置时获取纬度和经度0.0,第1张

概述我使用以下代码来获取我当前的位置.但我面临的问题是,纬度经度总是返回0.0.我已打开手机中的GPS设置并设置所有权限.但我仍然面对这个问题. locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);customLocationListener = new CustomLocationListe 我使用以下代码来获取我当前的位置.但我面临的问题是,纬度和经度总是返回0.0.我已打开手机中的GPS设置并设置所有权限.但我仍然面对这个问题.
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);customLocationListener = new CustomLocationListener();locationManager.requestLocationUpdates(        LocationManager.GPS_PROVIDER,customLocationListener);class CustomLocationListener implements LocationListener{ ............      public voID onLocationChanged(Location argLocation) {          if(location != null) {             int latitude=(int)(argLocation.getLatitude()*1E6);           int longitude=(int)(argLocation.getLongitude()*1E6);              }       } ........ }

你们中的任何人都知道为什么吗?

注意:我的活动扩展了MapActivity而不是Location Listener

解决方法 试试这种方式
locationManager = (LocationManager) context                .getSystemService(Context.LOCATION_SERVICE);        Criteria locationCritera = new Criteria();        String provIDername = locationManager.getBestProvIDer(locationCritera,true);        if(provIDername!=null)            location = locationManager.getLastKNownLocation(provIDername);        locationListener = new MyLocationListener();        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,locationListener);

在mylocationListener中设置您的位置对象

private class MyLocationListener implements LocationListener {    public voID onLocationChanged(Location loc) {        if (loc != null) {            location = loc;         }    }    public voID onProvIDerDisabled(String provIDer) {    }    public voID onProvIDerEnabled(String provIDer) {    }    public voID onStatusChanged(String provIDer,int status,Bundle extras) {    }}
总结

以上是内存溢出为你收集整理的android – 在地图中显示当前位置时获取纬度和经度0.0全部内容,希望文章能够帮你解决android – 在地图中显示当前位置时获取纬度和经度0.0所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1134429.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存