2014-09-25 5 views
2

안녕하세요 나는 자전거가 움직이는 동안 안드로이드에서 GPS를 사용하여 거리를 계산해야합니다. 나는 aprx 올바른 값을주고 에뮬레이터에서 위도와 경도 값을주고 있어요 그러나 장치의 패킷 데이터를 사용하고 앱을 실행하면 값이 움직임없이 바뀝니다. 이 코드입니다, 제발 제안하십시오.안드로이드 (이동 중에)에서 이동 거리를 계산

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     showCurrentLocation(); 
     b2=(Button)findViewById(R.id.button2); 
     b2.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       clearButton(); 
      } 
    });  
    } 

    @SuppressWarnings("unused") 
    protected void showCurrentLocation() { 
     // Getting LocationManager object 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     // Creating an empty criteria object 
     Criteria criteria = new Criteria(); 
     // Getting the name of the provider that meets the criteria 
     provider = locationManager.getBestProvider(criteria, false); 

     if (provider != null && !provider.equals("")) { 
      Location location = locationManager.getLastKnownLocation(provider); 

      locationManager.requestLocationUpdates(provider, 20000, 1, this); 
      clat = location.getLatitude(); 
      clon = location.getLongitude(); 
      a[0] = clat; 
      a[1] = clon; 

      if (location != null) 
       onLocationChanged(location); 
      else 
       Toast.makeText(getBaseContext(), "Location can't be retrieved", 
         Toast.LENGTH_SHORT).show(); 

     } else { 
      Toast.makeText(getBaseContext(), "No Provider Found", 
        Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // Getting reference to TextView tv_longitude 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     // Creating an empty criteria object 
     Criteria criteria = new Criteria(); 
     // Getting the name of the provider that meets the criteria 
     provider = locationManager.getBestProvider(criteria, false); 

     if (provider != null && !provider.equals("")) { 
      // Get the location from the given provider 
      Location location1 = locationManager.getLastKnownLocation(provider); 
      locationManager.requestLocationUpdates(provider, 20000, 1, this); 
      plat = location.getLatitude(); 
      plon = location.getLongitude(); 
      a[2] = plat; 
      a[3] = plon; 
      if(a[0]!=a[2]||a[1]!=a[3]) 
     { 
       dis += getDistance(a); 
     } 

      a[0] = plat; 
      a[1] = plon; 
      Log.e("aa", String.valueOf(dis)); 
      TextView tvDistance = (TextView) findViewById(R.id.textView1); 
      tvDistance.setText(String.valueOf(dis) + " km"); 
     } 
    } 
    public double getDistance(double a[]) { 
    double earthRadius = 6371; //kilometers 
    double dLat = Math.toRadians(a[2] -a[0]); 
    double dLng = Math.toRadians(a[3] -a[1]); 
    double b = Math.sin(dLat/2) * Math.sin(dLat/2) + 
       Math.cos(Math.toRadians(a[0])) * Math.cos(Math.toRadians(a[2])) * 
       Math.sin(dLng/2) * Math.sin(dLng/2); 
    double c = 2 * Math.atan2(Math.sqrt(b), Math.sqrt(1-b)); 
    float dist = (float) (earthRadius * c); 

    return dist; 
    } 

    protected void clearButton(){ 
     dis=0; 
     showCurrentLocation(); 
    } 
+0

안녕하세요, getLastKnownLocation()을 (를) 사용하고 있습니다. 마지막으로 업데이트 된 장소에서 취할 것이므로 잘못된/잘못된 위치 쌍을 제공합니다. 더 좋은 방법은 googleplayservices lib를 사용하는 것입니다. 그것은 당신이 움직이는 속도와 정확한 위치를 얻는 것을 도울 것입니다. Google 거리 API 인 – Sivakumar

+0

googleplayservices lib를 사용하여 현재 위치와 지정 위치 사이의 거리를 찾을 수 있습니다. Google Play 서비스 라이브러리는 Nokia, Amazon 및 기타 Google Android 장치가 아닌 곳에 배포하지 못하도록합니다. 당신이 말하는 버그가 g에 의해 원한 것인지 궁금합니다. – Benoit

+0

@akhila 나는 같은 문제에 직면하고있다. 당신은 해결책을 얻었습니까? .. –

답변

0

당신은 단계를하고 위치가 다릅니다하지 않을 경우 getDistance를 호출하지만, 경과 시간은 자전거 속도와 관련이 때 수 (1 초?).

private long timestamp = 0; 

public double getDistance(double a[]) { 
    timestamp = System.currentTimeMillis(); 
    (....) 
} 


public void onLocationChanged(Location location) { 
    (...) 
    if(System.currentTimeMillis() - timestamp > 1000){ 
      dis += getDistance(a); 
    }