簡體   English   中英

從字符串數組創建Android位置

[英]Create an Android location from a string array

我正在開發一個Android應用程序。 我需要比較位置並查看它們之間的距離。 但是,我有很多。 我發現的最簡單的方法是將它們存儲在字符串數組中,然后進行比較。 我已經搜索過,但是還沒有找到一種將項目從字符串數組轉換為位置的方法。 我試過了:

double distance

Location locationA = new Location(“point A”)

locationA.setLatitude(latitude);

locationA.setLongitude(Longitude);

Location locationB = new Location(“point B”);

locationB.setLatitude(Latitude.get(0));

LocationB.setLongitude(latitude.get(0));

distance = locationA.distanceTo(locationB);

但這行不通。 我的代碼發布在下面:

NearestStations.java

public class Neareststations extends Activity implements LocationListener {
LocationManager mLocationManager;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.neareststations);
    // Show the Up button in the action bar.
    getActionBar().setDisplayHomeAsUpEnabled(true);



        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        Log.i("MYTAG", String.valueOf(longitude));
        Log.i("MYTAG", String.valueOf(latitude));
        if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
            // Do something with the recent location if it is less than two minutes old
            Log.i("MYTAG", String.valueOf(location));
            Log.i("MYTAG", String.valueOf(longitude));
            Log.i("MYTAG", String.valueOf(latitude));
        }
        else {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 50, this);
            Log.i("MYTAG", String.valueOf(location));
        }
    }

    public void onLocationChanged(Location location) {
        if (location != null) {
        // Do something withthe current location
            Log.i("MYTAG", String.valueOf(location));

        }
    }

    public void onProviderDisabled(String arg0) {}
    public void onProviderEnabled(String arg0) {}
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

    String[] Stations = getResources().getStringArray(R.array.Stations);
    String[] Longitude = getResources().getStringArray(R.array.Longitude);
    String[] Latitude = getResources().getStringArray(R.array.Latitude);

    Map<String, String> myMap = new HashMap<String, String>();{
    for (int i = 0; i <8; i++) {
        myMap.put(Stations[i], Latitude[i]);
    }
    }

    Map<String, String> myMap1 = new HashMap<String, String>();{
    for (int h = 0; h <8; h++) {
        myMap1.put(Stations[h], Longitude[h]);
    }
    }

我怎樣才能做到這一點? 我的字符串數組是標准的,並且具有諸如

<item>-87.669147</item>
<item>-87.680622</item>

謝謝您的幫助。

字符串需要轉換為雙精度。 另外,比較兩個點時應使用distanceBetween(),但distanceTo()對於較小的距離也可以正常工作。

這是將String值轉換為double以便與Location結合使用的基本示例。 距離將以米為單位返回,因此如果要英里,請乘以0.000621371192237334。

double distance;  

String str_lat_start = "29.287260";
String str_lon_start = "-81.100327";
String str_lat_end = "26.016045";
String str_lon_end = "-80.150169";

double lat_start = 0;
double lon_start = 0;
double lat_end = 0;
double lon_end = 0;

try {

    lat_start = Double.parseDouble( str_lat_start ); 
    lon_start = Double.parseDouble( str_lon_start );
    lat_end = Double.parseDouble( str_lat_end );
    lon_end = Double.parseDouble( str_lon_end );

} catch (NumberFormatException e) {
    Log.v("Main", "Convert to Double Failed : ");
}

Location locationA = new Location("point A");  
locationA.setLatitude( lat_start );  
locationA.setLongitude( lon_start );  

Location locationB = new Location("point B");  
locationB.setLatitude( lat_end );  
locationB.setLongitude( lon_end );  

distance = locationA.distanceTo(locationB) * 0.000621371192237334; 
float[] results = new float[3];
    Location.distanceBetween(locationA.getLatitude(), locationA.getLongitude(), locationB.getLatitude(), locationB.getLongitude(), results);

A和B之間的距離是results[0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM