2018年12月7日 星期五

[Android]_ch9.12_GoogleMap計算兩點距離





[Android]_ch9.12_GoogleMap計算兩點距離





計算2點間距離
呼叫Location.distanceBetween(double
startLatitude,
double
startLongitude,
double
endLatitude,
double
endLongitude,
float[] results)
並傳入起點與終點的緯經度可以算出距離(公尺),計算結果存入results參數

btn1.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
String name=et1.getText().toString();
Geocoder gc=new Geocoder(MainActivity.this);
List<Address> list=null;
Address address=null;
try {
list=gc.getFromLocationName(name, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(list==null || list.isEmpty())
Toast.makeText(MainActivity.this, "找不到該位置!", Toast.LENGTH_SHORT).show();
else{
address=list.get(0);
}
double startLatitude=myLocation.getLatitude();
double startLongitude=myLocation.getLongitude();
double endLatitude=address.getLatitude();
double endLongitude=address.getLongitude();
float results[]=new float[1];
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results);
tv2.setText("距離"+name+": "+results[0]+" 公尺");
}
});

沒有留言:

張貼留言

Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...