这里我没有使用谷歌地图。是否有必要使用谷歌地图找出两点之间的距离?我使用gecoder得到两点。所以我认为这就够了。我没有得到返回值被调用的方法。请看下面的代码
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class WhereDoYouLive extends Activity {
/** Called when the activity is first created. */
double latitude ;
double longitude ;
double latitude2 ;
double longitude2 ;
String sDistance;
int iDistance;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Set up GUI
final EditText addressfield = (EditText) findViewById(R.id.address); // Reference edit field
final EditText addressfield2 = (EditText) findViewById(R.id.secaddress);
final Button launchmapbtn = (Button) findViewById(R.id.launchmap); // Reference search button
final TextView lat=(TextView)findViewById(R.id.lat);
final TextView lon=(TextView)findViewById(R.id.lon);
final TextView lat2=(TextView)findViewById(R.id.lat2);
final TextView lon2=(TextView)findViewById(R.id.lon2);
final TextView result=(TextView)findViewById(R.id.res);
String lt2;
launchmapbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// List<Address> addresses=null;
String aa=addressfield.getText().toString();
String bb=addressfield2.getText().toString();
Geocoder geocoder = new Geocoder(getApplicationContext(),Locale.getDefault());
try {
List<Address> addresses;
addresses = geocoder.getFromLocationName(aa, 1);
if(addresses.size() > 0) {
latitude= addresses.get(0).getLatitude();
longitude= addresses.get(0).getLongitude();
String lt=Double.toString(latitude);
String lg=Double.toString(longitude);
lat.setText(lt);
lon.setText(lg);
}
List<Address> addresses2 ;
addresses2 = geocoder.getFromLocationName(bb, 1);
if(addresses2.size() > 0) {
latitude2= addresses2.get(0).getLatitude();
longitude2= addresses2.get(0).getLongitude();
String lt2=Double.toString(latitude2);
String lg2=Double.toString(longitude2);
lat2.setText(lt2);
lon2.setText(lg2);
}
int Roaddistance=GetDistance(latitude, longitude, latitude2, longitude2);
String fd=Integer.toString(Roaddistance);
result.setText(fd);
} catch (Exception e){
}
}
});
}
public int GetDistance(double lat1, double lon1, double lat2, double lon2) {
Log.d("lat1", Double.toString(lat1));
Log.d("lon", Double.toString(lon1));
Log.d("lat1", Double.toString(lat1));
Log.d("lon", Double.toString(lon1));
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json?");
urlString.append("origin=");//from
urlString.append( Double.toString(lat1));
urlString.append(",");
urlString.append( Double.toString(lon1));
urlString.append("&destination=");//to
urlString.append( Double.toString(lat2));
urlString.append(",");
urlString.append( Double.toString(lon2));
urlString.append("&mode=walking&sensor=true");
Log.d("xxx","URL="+urlString.toString());
// get the JSON And parse it to get the directions data.
try {
HttpURLConnection urlConnection= null;
URL url = null;
url = new URL(urlString.toString());
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
InputStream inStream = urlConnection.getInputStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));
String temp, response = "";
while((temp = bReader.readLine()) != null){
//Parse data
response += temp;
}
//Close the reader, stream & connection
bReader.close();
inStream.close();
urlConnection.disconnect();
//Sortout JSONresponse
JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
JSONArray array = object.getJSONArray("routes");
//Log.d("JSON","array: "+array.toString());
//Routes is a combination of objects and arrays
JSONObject routes = array.getJSONObject(0);
Log.d("JSON","routes: "+routes.toString());
String summary = routes.getString("summary");
Log.d("JSON","summary: "+summary);
JSONArray legs = routes.getJSONArray("legs");
Log.d("JSON","legs: "+legs.toString());
JSONObject steps = legs.getJSONObject(0);
//Log.d("JSON","steps: "+steps.toString());
JSONObject distance = steps.getJSONObject("distance");
Log.d("JSON","distance: "+distance.toString());
sDistance = distance.getString("text");
iDistance = distance.getInt("value");
Log.d("distance: ",sDistance);
Log.d("distance: ",Integer.toString(iDistance));
} catch (Exception e) {
// TODO: handle exception
}
Log.d("distance: ",sDistance);
Log.d("distance: ",Integer.toString(iDistance));
return iDistance ;
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lat2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:text="Enter your address"
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<EditText
android:text="Enter your secondaddress"
android:id="@+id/secaddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<Button
android:text="Search"
android:id="@+id/launchmap"
android:layout_width="150px"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
android manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.book.wheredoyoulive"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="Search Pro" >
<activity android:name=".WhereDoYouLive"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:-1)
使用location1.distanceto(location2)获取以米为单位的值。其中location1和location2分别是源和目的地。 并且没有必要使用谷歌地图来找到这个距离。 干杯