2016-09-13 5 views
1

私はGPSから位置情報を取得するマップアプリケーションを作成していますが、私はrequestLocationUpdates()から位置情報を取得していますが、GPSプロバイダからの応答はありません私はネットワークプロバイダを使用して私が場所を取得している、誰かが私が間違っている場所で助けることができるGPSからのrequestLocationUpdates()の場所が見つからない

また、私は自分のコードを最適化するためのヒントをありがとう)あまり混乱で、ここで

は私のコードです:

public void trackLocation(){ 
    if (ContextCompat.checkSelfPermission(activity.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE); 
     boolean flag; 
     Boolean dlg; 
     flag = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     if (flag) { 
      Log.v("function", "onClick"); 
      myLocationListener = new MyLocationListener(); 
      dlg = true; 
     } else { 
      dlg = gpsDialog(); 
      progressBar.setVisibility(View.GONE); 
     } 

     Log.d("maps", "Requesting location updates"); 
     if(dlg) { 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener); 
     } 
    } 
} 

public class MyLocationListener implements LocationListener { 

    @Override 
    public void onLocationChanged(Location location) { 
     if (ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     Log.d("maps", "on location changed : "+location.getLatitude() + "---"+ location.getLongitude()); 
     locationManager.removeUpdates(myLocationListener); 
    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 
     Log.d("maps" , "Status Changed"); 
    } 

    @Override 
    public void onProviderEnabled(String s) { 
     Log.d("maps" , "Provider Enabled"); 
    } 

    @Override 
    public void onProviderDisabled(String s) { 
     Log.d("maps" , "Provider Disabled"); 
    } 
} 

public Boolean gpsDialog(){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(activity); 
    builder.setMessage("Do you want to start GPS?") 
      .setCancelable(false) 
      .setTitle("GPS DISABLED") 
      .setPositiveButton("Yes", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          // finish the current activity 
          Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
          activity.startActivity(myIntent); 
          dialog.cancel(); 
         } 
        }) 
      .setNegativeButton("No", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          // cancel the dialog box 
          dialog.cancel(); 
         } 
        }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
    progressBar.setVisibility(View.GONE); 
    return false; 
} 

答えて

1

グーグルマップはlastknownLocationが不明な場合、それは簡単である方、GPSまたはNetworkを言う別のプロバイダを介して位置を取得するために時間がかかり、ユーザーのlastKnownLocation場所のため使用しています。 lastKnownLocationをベースロケーションとして使用し、LocationListenerでアップデートすることをお勧めします。それは処理場所の最も最適化された方法です。

 if (ActivityCompat.checkSelfPermission 
       (this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
       && 
       ActivityCompat.checkSelfPermission 
         (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
     { 
      requestPermissions(new String[]{ 
        Manifest.permission.ACCESS_COARSE_LOCATION, 
        Manifest.permission.ACCESS_FINE_LOCATION 
      }, 1); 

     } 
     else { 

      // enable location buttons 
      googleMap.setMyLocationEnabled(true); 
      googleMap.getUiSettings().setMyLocationButtonEnabled(true); 

      // fetch last location if any from provider - GPS. 
      final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
      final Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      //if last known location is not available 
      if (loc == null) { 

       final LocationListener locationListener = new LocationListener() { 
        @Override 
        public void onLocationChanged(final Location location) { 

         // getting location of user 
         final double latitude = location.getLatitude(); 
         final double longitude = location.getLongitude(); 
         //do something with Lat and Lng 
        } 

        @Override 
        public void onStatusChanged(String provider, int status, Bundle extras) { 
        } 

        @Override 
        public void onProviderEnabled(String provider) { 
         //when user enables the GPS setting, this method is triggered. 
        } 

        @Override 
        public void onProviderDisabled(String provider) { 
         //when no provider is available in this case GPS provider, trigger your gpsDialog here. 
        } 
       }; 

       //update location every 10sec in 500m radius with both provider GPS and Network. 

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10*1000, 500, locationListener); 
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 500, locationListener); 
      } 
      else { 
       //do something with last known location. 
       // getting location of user 
       final double latitude = loc.getLatitude(); 
       final double longitude = loc.getLongitude(); 
      } 
     } 

許可取扱い:onLocationChangedで

 @Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    switch (requestCode) { 

     case 1: 
      if (grantResults[0] != PackageManager.PERMISSION_GRANTED){ 
       //do something 
      } 
     default: 
      super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    } 
} 
+0

私は言ったように、私は以前にlastknownlocationを使用しました。それはいつも正しいとは言いませんが、私は同意しますが、毎回の精度が必要です –

+0

@DeepakJoshi 1年前の 'Endomondo'では' LocationListener'を使って場所をフィルタリングするのに最も最適な方法です。 lastKnownLocationを使用しない場合は、私のコードでlastknowlocationのメソッドを削除してください。これはうまくいきます。 – W4R10CK

+1

ありがとう、それは働いた、私は間違ったif自己申告の確認のための声明を書いた –

1

あなたのコードは正しいです。 GPSは通常建物内に情報を提供しません。外部にアプリをテストしようとすると結果が得られます。

+0

()ログ内の位置を印刷イムが、GPSプロバイダの場合はそのショーの何が、ネットワークプロバイダにそのログ –

0
firstly check gps_enabled return true or not. 

LocationManager lm = (LocationManager) mCtx 
       .getSystemService(Context.LOCATION_SERVICE); 

gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

By this code you get accurate lat , long 


Location net_loc = null, gps_loc = null, finalLoc = null; 

if (gps_enabled) 
    gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
if (network_enabled) 
    net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

if (gps_loc != null && net_loc != null) { 

    if (gps_loc.getAccuracy() >= net_loc.getAccuracy()) 
     finalLoc = gps_loc; 
    else 
     finalLoc = net_loc; 

     // I used this just to get an idea (if both avail, its upto you which you want to take as I taken location with more accuracy) 

} else { 

    if (gps_loc != null) { 
     finalLoc = net_loc; 
    } else if (net_loc != null) { 
     finalLoc = gps_loc; 
    } 
} 
+0

に私はあなたに感謝することを提供します助けていただきありがとうございますが、GPSの電源を切ってある場所から別の場所に移動してGPSをもう一度オンにすると、GPSの最後の場所が取得されるため、getLastKnownLocation()を使用したくありません。古い場所は、新しい場所を要求するのではなく、最初の数回の試行のための座標です。以前はそれを使用していましたが、それは私に起こりました。私はそう思うでしょう。 –

関連する問題