android - android中的米数计算?

标签 android distance google-places

我想在android程序中显示两个城市之间的里程,为此我写了下面的代码:

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();  
        }  
    }  
}  

我正在获取地点,但现在我想显示两个城市之间的距离。

我该如何继续这个项目,或者有什么替代方法可以实现这个目标吗? 请将任何有用的链接发送给我,以便我继续前进。

最佳答案

有内置代码可以找到两个位置之间的距离,

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

Refer this link for Location class

关于android - android中的米数计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13151760/

相关文章:

Android:从底部延迟按下按钮

matlab - 取决于距离函数,matlab 中的 k-means 内存不足?

google-places-api - google-places-API 中已弃用的杂货店或超市类型的新类型

json - Google 地方信息自动完成功能可以在标签编辑器字段中使用吗?

android - 将 PNG 文件放在哪里以及如何从 XML 布局中的 ImageView 引用它们?

Android OpenGL fragment 着色器分辨率

java - 使用 JSON Parsing with volley 更新 ListView 的最佳方法

python - 距阵列的最小距离

python - 一种有效计算一个标记像素与其最近的不同标记像素的距离的算法

python - 我的谷歌地图 api key 在 python 中不起作用,尽管它在 swift 中工作