2017-03-31 58 views
0

我已按照本教程http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/显示弹出如果GPS未启用并获取用户当前位置。GPS启用后获取用户当前的经纬度 - Android

这里是广播接收器当我通过设置对话框或从下拉菜单中启用GPS,它会监听启用禁用状态

private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) { 
      gps = new GPSTracker(getActivity()); 

      if(gps.canGetLocation()){ 
       Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude()); 

       Toast.makeText(getActivity(), "GPS Switch", Toast.LENGTH_SHORT).show(); 

       if(gps.getLatitude() != 0 && gps.getLongitude() != 0){ 
        final CameraPosition cameraPosition = new CameraPosition.Builder() 
          .target(new LatLng(gps.getLatitude(),gps.getLongitude()))  // Sets the center of the map to selected place 
          .zoom(15)     // Sets the zoom 
          .build();     // 
        gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
        Toast.makeText(getActivity(), "GPS Switch Lat: "+gps.getLatitude()+" Long: "+gps.getLongitude(), Toast.LENGTH_SHORT).show(); 
       } 
      } else { 
       if(!gps.isSettingDialogShown()) 
        gps.showSettingsAlert(); 
      } 
     } 
    } 
}; 

的GPS。 GPSTracker类获取用户当前位置的定位服务,并检查在此代码片段

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 

     // getting GPS status 
     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     // getting network status 
     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 
     } else { 
      this.canGetLocation = true; 
      if (isNetworkEnabled) { 
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       Log.d("Network", "Network Enabled"); 
       if (locationManager != null) { 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 

        } 
       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (isGPSEnabled) { 
       if (location == null) { 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("GPS", "GPS Enabled"); 
        if (locationManager != null) { 
         location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 

         } 
        } 
       } 
      } 
     } 

    } catch (SecurityException e) { 
     e.printStackTrace(); 
    } 

    return location; 
} 

问题

首先,我不知道为什么广播接收器,接收动作两次;其次当在正常模式下,接收器的经纬度值保持为零,但在调试时有时会返回用户当前位置的值。

你能帮我做什么错吗?

+2

https://stackoverflow.com/a/43082164/115145 – CommonsWare

+0

使用谷歌Fused Location Api和位置设置,[检查此](http:///stackoverflow.com/questions/43138788/ask-user-to-turn-on-location/43139553#43139553) –

+0

@CommonsWare我不明白我打开GPS后如何获得广播接收机的经纬度?在你的回答中,你说避免使用getLastKnownLocation(),那么使用什么而不是这个? –

回答

0

如果您不需要单独使用Gps和网络位置。我建议使用FusedLocationProvider。 BroadcastReceiver可能工作两次,因为它适用于全球定位系统和网络位置服务

+0

感谢您的帮助,它解决了我面临的问题。 –

相关问题