2017-03-31 5 views
0

GPSが有効になっておらず、ユーザーの現在地を取得している場合、私はこのチュートリアルhttp://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/に従っています。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に耳を傾け放送受信機です。 2回および第二に、まず

は、私がなぜ放送受信機を知らないアクションを受け取る: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

Googleの融合ロケーションApiとロケーションの設定[こちらをチェック] /stackoverflow.com/questions/43138788/ask-user-to-turn-on-location/43139553#43139553) –

+0

@CommonsWare GPSをオンにした後に放送受信機で緯度経度を取得する方法がわかりませんでしたか?そして、あなたの答えでは、これの代わりに何を使うべきかをgetLastKnownLocation()を使うのを避けるように言ったのですか? –

答えて

0

GPSとネットワークの別々の場所で作業する必要がない場合。 FusedLocationProviderを使用することをお勧めします。 BroadcastReceiverはおそらくGPSとネットワークロケーションサービスの両方で動作するため、2回動作している可能性があります

+0

私が直面していた問題を解決してくれてありがとう。 –

関連する問題