首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从字符串数组创建Android位置

从字符串数组创建Android位置
EN

Stack Overflow用户
提问于 2013-10-09 21:18:37
回答 2查看 5K关注 0票数 1

我正在开发一个Android应用程序。我需要比较一下地点,看看它们之间的距离。不过,我有很多。我发现的最简单的方法是将它们存储在字符串数组中,然后比较它们。我已经搜索过了,但是我还没有找到一种方法将一个项从字符串数组生成到一个位置。我试过:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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]);
    }
    }

我该怎么做?我的字符串数组是标准的,有一些条目,如

代码语言:javascript
运行
复制
<item>-87.669147</item>
<item>-87.680622</item>

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-09 21:49:37

字符串需要转换成双倍。另外,在比较两个点时,您应该使用distanceBetween(),但是distanceTo()在较小的距离上工作得很好。

下面是一个将字符串值转换为双倍以便与位置一起使用的基本示例。距离将以米为单位返回,所以如果你想要的话,乘以0.000621371192237334。

代码语言:javascript
运行
复制
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; 
票数 5
EN

Stack Overflow用户

发布于 2013-10-09 22:00:32

代码语言:javascript
运行
复制
float[] results = new float[3];
    Location.distanceBetween(locationA.getLatitude(), locationA.getLongitude(), locationB.getLatitude(), locationB.getLongitude(), results);

A和B之间的距离是results[0]

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19282948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档