2012-10-31 29 views
1

我想在Android程序顯示兩個城市之間的里程中,我寫了下面的代碼:在android中進行Millage計算?

package com.example.distancebcities; 

import java.io.IOException; 
import java.util.List; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import android.location.Address; 
import android.location.Geocoder; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 

public class MainActivity extends MapActivity { 
    //extending the MapActivity here 
    Geocoder geocoder = null; 
    MapView mapView = null; 

    @Override 
    //location display 
    protected boolean isLocationDisplayed() { 
     return false; 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mapView = (MapView) findViewById(R.id.geoMap); 
     mapView.setBuiltInZoomControls(true); 

     // lat/long of Jacksonville, FL 
     int lat = (int) (30.334954 * 1000000); 
     int lng = (int) (-81.5625 * 1000000); 

     GeoPoint pt = new GeoPoint(lat, lng); 
     mapView.getController().setZoom(10); 
     mapView.getController().setCenter(pt); 
     geocoder = new Geocoder(this);    
    } 

    public void doClick(View arg0) {        
     try { 
      EditText loc = (EditText) findViewById(R.id.location); 

      //getting the dittext id 

      String locationName = loc.getText().toString(); 
      List<Address> addressList = geocoder.getFromLocationName(locationName, 5); 
      if (addressList != null && addressList.size() > 0) { 
      int lat = (int) (addressList.get(0).getLatitude() * 1000000); 
      int lng = (int) (addressList.get(0).getLongitude() * 1000000); 

      //getting the lat and long postions of the cities 

      GeoPoint pt = new GeoPoint(lat, lng); 
      mapView.getController().setZoom(15); 
      mapView.getController().setCenter(pt); 

      //zooming and display the place of the edit text entered value . 
     } 

     catch (IOException e){ 
      e.printStackTrace(); 
     } 
    } 
} 

我得到的地方,但現在我想兩個城市之間顯示的距離。

我該如何繼續執行此程序,或者是否有其他方法來實現此目的? 請發送任何有用的鏈接給我,讓我可以前進。

回答

0

對於查找兩個位置之間的距離,請使用以下代碼。

public double CalculationByDistance(Location Start, Location End) 
{ 
     double Radius = 6371; 
     double lat1 = Start.getLatitude(); 
     double lat2 = End.getLatitude(); 
     double lon1 = Start.getLongitude(); 
     double lon2 = End.getLongitude(); 
     double dLat = Math.toRadians(lat2 - lat1); 
     double dLon = Math.toRadians(lon2 - lon1); 
     double a = Math.sin(dLat/2) * Math.sin(dLat/2) 
       + Math.cos(Math.toRadians(lat1)) 
       * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon/2) 
       * Math.sin(dLon/2); 
     double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
     double km = Radius * c; 
     return km * 1000; 
} 

這個距離將在Meters

+0

我在哪裏可以把這個代碼在上面程序? – user1787493

+0

這是方法。你必須通過開始和結束位置,它會返回兩個位置之間的距離(米)。你可以放在任何地方 –

+0

我無法學習你的編程,我只是幫你.. –

1

返回有內置的代碼,找出兩個位置之間的距離,

distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

Refer this link for Location class