android中的Millage计算?
我想在android程序中显示两个城市之间的里程,为此我编写了以下代码:android中的Millage计算?,android,distance,google-places,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;
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();
}
}
}
package com.example.distancebcities;
导入java.io.IOException;
导入java.util.List;
导入com.google.android.maps.GeoPoint;
导入com.google.android.maps.MapActivity;
导入com.google.android.maps.MapView;
导入android.location.Address;
导入android.location.Geocoder;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.view.view;
导入android.widget.EditText;
公共类MainActivity扩展了MapActivity{
//在此扩展MapActivity
地理编码器地理编码器=空;
MapView MapView=null;
@凌驾
//位置显示
受保护的布尔值isLocationDisplayed(){
返回false;
}
@凌驾
受保护的布尔值isRouteDisplayed(){
返回false;
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView=(mapView)findViewById(R.id.geoMap);
mapView.SetBuilTinZoomControl(真);
//佛罗里达州杰克逊维尔市拉特/朗
int-lat=(int)(30.334954*1000000);
国际液化天然气=(国际)(81.5625*1000000);
地质点pt=新的地质点(纬度、液化天然气);
mapView.getController().setZoom(10);
mapView.getController().setCenter(pt);
地理编码器=新地理编码器(本);
}
公共void doClick(视图arg0){
试试{
EditText loc=(EditText)findViewById(R.id.location);
//获取文本id
String locationName=loc.getText().toString();
List addressList=geocoder.getFromLocationName(locationName,5);
如果(addressList!=null&&addressList.size()>0){
intlat=(int)(addressList.get(0.getLatitude()*1000000);
int lng=(int)(addressList.get(0.getLongitude()*1000000);
//获得城市的横向和纵向职位
地质点pt=新的地质点(纬度、液化天然气);
mapView.getController().setZoom(15);
mapView.getController().setCenter(pt);
//缩放并显示编辑文本输入值的位置。
}
捕获(IOE){
e、 printStackTrace();
}
}
}
我得到了位置,但现在我想显示两个城市之间的距离
我如何进行该计划,或者是否有任何替代方案来实现这一目标?
请将任何有用的链接发送给我,以便我可以继续前进。要查找两个位置之间的距离,请使用下面的代码
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;
}
此距离将以
米为单位返回
以查找两个位置之间的距离,请在代码下方使用
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;
}
此距离将以米为单位返回有内置的代码来查找两个位置之间的距离
distance-between(双震惊度、双震惊度、双经纬度、双经纬度、浮点[]结果)
有一个内置的代码来查找两个位置之间的距离
distance-between(双震惊度、双震惊度、双经纬度、双经纬度、浮点[]结果)
这是一种方法。您只需通过开始和结束位置,它将返回两个位置之间的距离(米)。您可以将此方法放在任何位置。您只需通过开始和结束位置,它将返回两个位置之间的距离(米)。您可以放在任何位置