Android 总是在开始时得到一个新的位置

标签 android location locationmanager

一如既往地获得新位置?我在 onCreate here

中使用此代码
        locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
               if (isGPSEnabled) {
                      if (location == null) {
                          provider = LocationManager.GPS_PROVIDER;
                          locationManager.requestLocationUpdates(provider, 500, 0, this);
                          if (locationManager != null) {
                              location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);                                                    
                              if (location != null) {
                                  latLng = new LatLng(location.getLatitude(), location.getLongitude());                                   
                                  if (location != null) {                                                                                                             
                                      ParseQueryMap();
                                  }
                              }
                          }
                       }
                     }                            
              if (isNetworkEnabled){ 
                       if (location == null) {
                       provider = LocationManager.NETWORK_PROVIDER;
                       locationManager.requestLocationUpdates(provider, 500, 0, this);
                       if (locationManager != null) {                   
                           location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);       
                           if (location != null) {
                               latLng = new LatLng(location.getLatitude(), location.getLongitude());                               
                               if (location != null) {  
                                   ParseQueryMap();                               
                               }
                            }
                         }
                       }
                     }
                }

如果在我运行应用程序时该位置不可用,则采用以前已知的位置 (getLastKnownLocation())。我不喜欢这个解决方案。我需要应用程序立即或在一段时间后(大约 5 秒)向我显示我的确切位置。启动时的确切位置我需要显示最近的基地点。是否可以?感谢您的帮助

最佳答案

尝试使用被动位置提供程序。通过使用它,您可以接收位置更新以及其他已请求位置更新的应用。

参见 this blog entry其中详细描述了如何尽快修复该位置。您可以在 android-protips-location 中找到示例代码项目。

关于Android 总是在开始时得到一个新的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16037665/

相关文章:

Android 非法参数异常 : already added

tomcat - java.io.FileNotFoundException(访问被拒绝),文件写在@MultipartConfig(location)中

android - 如何将数据库从我的 android 拉到我的桌面上?

android - 如何阻止用户长时间等待位置修复?

android - GEOFENCE TRANSITION DWELL 不适用于 android

android - Proguard 错误代码 1 : Proguard. ParseException

android - 使用 WebViewClient shouldOverrideUrlLoading 时,WebChromeClient 中的链接不会打开 safari

android - 在服务调用的线程中获取上下文

android - 当设备进入休眠状态时,位置管理器是否继续工作?

Android-相同的 GPS 位置重复而不更新