2013-11-01 16 views
5

こんにちは私は、十分に正確なユーザーの現在地を欲しいAndroidアプリケーションを開発しています。だから私はこの1つを実装する2つのオプションがありますユーザーの現在の位置を取得するandroid

現在の場所を取得します。 http://developer.android.com/training/location/receive-location-updates.html

場所アップデートを受けhttp://developer.android.com/training/location/retrieve-current.html

は、だからここに私の問題です。私は現在の場所を取得することを終えました。そして、私は最終的にユーザーに最後の最もよく知られた場所を与えてくれます。しかし、私の要件は、ユーザーの現在の場所です。

私は受取場所更新の方に移動します。私は最初の更新を得ることができますが、私は更新を停止します。しかし、ここでも初めて、新しい場所ではなく最後の場所が与えられます。最初の読み込み後にgpsを開始して連続的な更新を行います。しかし、私は最初の更新だけに興味があります。

私はユーザーに正確な現在地を与える良い解決策が必要です。どちらかgpsまたはネットワークを使用するか、または使用可能ないずれかを使用しています。

私が間違ったことをしている場合は、正しく入力してください。助けが必要。ありがとうございました。

答えて

1

GPS listener solutionが必要です。

ロケーションリスナーに追加しますが、GPSを選択してください。 public void onLocationChanged(Location loc)のみ現在の場所を保持します。

+0

ありがとうございました。私はあなたの解決策を欲しい。しかし、私が感じることはそれほど柔軟性を提供しません。私はAPIのことを言いますが、それらも位置情報プロバイダの世話をします。つまり、gpsが利用できない場合、自動的に他のプロバイダを使用して場所を取得します。だから私はそれらの機能を欠場するでしょう。間違いなくあなたのソリューションを試してみます。 – nilkash

2

これは、ユーザーの現在地をGoogleマップに使用する方法です。

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      mMap.setMyLocationEnabled(true); 
      Criteria criteria = new Criteria(); 
      LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
      String provider = locationManager.getBestProvider(criteria, false); 
      Location location = locationManager.getLastKnownLocation(provider); 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      LatLng coordinate = new LatLng(lat, lng); 
      CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 13); 
      mMap.animateCamera(yourLocation); 

     } else { 
      final AlertDialog alertDialogGPS = new AlertDialog.Builder(getActivity()).create(); 

      alertDialogGPS.setTitle("Info"); 
      alertDialogGPS.setMessage("Looks like you have not given GPS permissions. Please give GPS permissions and return back to the app."); 
      alertDialogGPS.setIcon(android.R.drawable.ic_dialog_alert); 
      alertDialogGPS.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 

        Intent intentSettings = new Intent(Settings.ACTION_APPLICATION_SETTINGS); 
        startActivity(intentSettings); 
        alertDialogGPS.dismiss(); 
       } 

      }); 

      alertDialogGPS.show(); 
     } 
関連する問題