2016-11-08 10 views
0

速度計測アプリケーションでは、4秒ごとに緯度と経度の更新を取得する必要があります。私は、getLatitude関数とgetLongitude関数の2つの呼び出しの間にsleep関数を使用しました。しかし、それを画面に表示すると、倍数(小数点以下14桁)が同じ値を持つので、lon1とlon2の値が同じままであることがわかります。固定間隔の後に緯度の場所を取得

固定後の緯度と経度の取得方法時間間隔(ここでは4秒)? 事前に感謝します。

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
 

 
     locationListener = new LocationListener() 
 
     { 
 
      @Override 
 
      public void onLocationChanged(Location location) { 
 
       double r = 6371; 
 
       double lat1 = location.getLatitude(); 
 
       double lon1 = location.getLongitude(); // To get initial longitude 
 
       SystemClock.sleep(4000);    /////This seems ineffective. 
 
       double lat2 = location.getLatitude(); 
 
       double lon2 = location.getLongitude(); // and here 
 
       double dlat = (lat2-lat1)*22/7/180; 
 
       double dlon = (lon2-lon1)*22/7/180; 
 
       
 
       textView.append("lon1:"+lon1+" lon2:"+lon2+" Kmph\n"); 
 
       lat1 = lat1*22/7/180; 
 
       lat2 = lat2*22/7/180; 
 
       double h = ((1-Math.cos(dlat))/2) + Math.cos(lat1)*Math.cos(lat2)*(1-Math.cos(dlon))/2; 
 
       double d = 2**Math.asin(Math.sqrt(h)); 
 
       double sp = d/3; 
 
       textView.append(" "+ sp + " Kmph\n"); 
 
      } 
 

 
      
 

 
      @Override 
 
      public void onStatusChanged(String s, int i, Bundle bundle) { 
 

 
      } 
 

 
      @Override 
 
      public void onProviderEnabled(String s) { 
 

 
      } 
 

 
      @Override 
 
      public void onProviderDisabled(String s) { 
 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
 
       startActivity(intent); 
 
      } 
 
     }; 
 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ 
 
      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_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION, 
 
         Manifest.permission.INTERNET 
 
       },10); 
 
       return; 
 
      } 
 
     }else { 
 
      configureButton(); 
 
     } 
 

 
    } 
 

 
    @Override 
 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
 
     switch (requestCode) { 
 
      case 10: 
 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
 
        configureButton(); 
 
       return; 
 
     } 
 
    } 
 

 
    private void configureButton() { 
 
     button.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View view) { 
 
       locationManager.requestLocationUpdates("gps", 4000, 0, locationListener); 
 
      } 
 
     }); 
 

 
    } 
 
}

+0

は、あなただけの30 4 http://stackoverflow.com/questions/39314901/getting-latitude-and-longitude-in-30-secondsに – Saveen

+0

感謝を変更するには、この時に見ることができますが、これはで役立っていません現在および以前の緯度および経度の値を取り出す。 – Mani

+0

現在の値を取得するのに役立ちますが、以前の値が必要な理由は? – Saveen

答えて

0

ちょっと私にはよく分からないが、あなたは、これはあなたを助けるこの 希望のようなものを探しています。

float oldlat, oldlng; 
    float currentLat, currentLng; 


    // Use this in location changed 
    currentLat = (float) location.getLatitude(); 
    currentLng = (float) location.getLongitude(); 

    final Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
    @Override 
    public void run() { 

     oldlat = (float) location.getLatitude(); 
     oldlng = (float) location.getLongitude(); 

    } 
    }, 4000); 
関連する問題