android:获取两个地理位置之间路线的转弯方向
我想显示两个位置之间的转弯方向。 我试过了android:获取两个地理位置之间路线的转弯方向,android,google-maps,Android,Google Maps,我想显示两个位置之间的转弯方向。 我试过了 uri = "http://maps.google.com/maps?saddr=" + "lat1" + "," + "lon1" + "&daddr=" + "lat2" + "," + "long2"; Intent intent = new Intent(android.content.Intent.ACTION_VIEW
uri = "http://maps.google.com/maps?saddr="
+ "lat1" + ","
+ "lon1" + "&daddr="
+ "lat2" + "," + "long2";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(uri));
它给出了路线。
但我不想打开浏览器并显示。相反,有一种方法可以获取路线,以便它可以显示在我自己的布局中。在文本视图中查看ex。查看GreatMaps。它们具有检索路由的逻辑。它基于C#语言,只需使用它们的逻辑并用Java编写等价的代码
这些代码是我在毕业设计中写的,所以它们没有经过优化
public class MapService {
public static final String TAG = "[MapService]";
public static final String GET_DIRECTION_URL = "http://maps.google.com/maps/api/directions/json?" + "origin=%f,%f" + "&destination=%f,%f" + "&sensor=true&language=en&units=metric";
public static DirectionResult getDirectionResult(double latitude1, double longtitude1, double latitude2, double longtitude2) {
String url = String.format(GET_DIRECTION_URL, latitude1, longtitude1, latitude2, longtitude2);
Log.d("URL", url);
// parse direction
DirectionParser directionParser = new DirectionParser() {
@Override
public void onSuccess(JSONObject json) throws JSONException {
// process response status
String status = json.getString("status");
if (!status.equals("OK")) {
String error = null;
if (status.equals("NOT_FOUND") || status.equals("ZERO_RESULTS")) {
error = Global.application.getString(R.string.errCantGetDirection);
} else {
error = Global.application.getString(R.string.errGetDirection);
}
result.instructions = new String[] { error };
result.hasError = true;
return;
}
/*
* routes[] legs[] steps[] html_instructions
*/
JSONArray arrRoutes = json.getJSONArray("routes");
// no routes found
if (arrRoutes.length() == 0) {
result.instructions = new String[] { Global.application.getString(R.string.errCantGetDirection) };
result.hasError = true;
return;
}
JSONArray arrLegs = arrRoutes.getJSONObject(0).getJSONArray("legs");
JSONObject firstLeg = arrLegs.getJSONObject(0);
JSONArray arrSteps = firstLeg.getJSONArray("steps");
int len = arrSteps.length();
result.instructions = new String[len];
result.points = new LinkedList<GeoPoint>();
JSONObject leg = null;
// get instructions
for (int i = 0; i < len; ++i) {
leg = arrSteps.getJSONObject(i);
// location = leg.getJSONObject("start_location");
String encoded = leg.getJSONObject("polyline").getString("points");
result.points.addAll(decodePoly(encoded));
result.instructions[i] = Html.fromHtml(leg.getString("html_instructions")).toString();
Log.d("html_instructions", "" + Html.fromHtml(leg.getString("html_instructions")));
// result.points[i] = new GeoPoint(
// (int) (location.getDouble("lat") * 1E6),
// (int) (location.getDouble("lng") * 1E6));
}
// location = leg.getJSONObject("end_location");
// result.points[len] = new GeoPoint(
// (int) (location.getDouble("lat") * 1E6),
// (int) (location.getDouble("lng") * 1E6));
// distance and duration info
JSONObject distance = firstLeg.getJSONObject("distance");
if (distance != null) {
result.distance = distance.getString("text");
}
JSONObject duration = firstLeg.getJSONObject("duration");
if (duration != null) {
result.duration = duration.getString("text");
}
}
@Override
public void onFailure(String message) {
String error = "Error";
result.instructions = new String[] { error };
result.hasError = true;
}
};
// return direction result
RestClient.getData(url, directionParser);
return directionParser.result;
}
private static List<GeoPoint> decodePoly(String encoded) {
List<GeoPoint> poly = new ArrayList<GeoPoint>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) lng / 1E5) * 1E6));
poly.add(p);
}
return poly;
}
}
公共类映射服务{
公共静态最终字符串标记=“[MapService]”;
公共静态最终字符串GET\u DIRECTION\u URL=”http://maps.google.com/maps/api/directions/json?“+”原点=%f,%f“+”&目标=%f,%f“+”&传感器=true&语言=en&单位=metric”;
公共静态方向结果getDirectionResult(双纬度1、双纬度1、双纬度2、双纬度2){
字符串url=String.format(获取方向url、纬度1、纬度1、纬度2、纬度2);
Log.d(“URL”,URL);
//解析方向
DirectionParser DirectionParser=新的DirectionParser(){
@凌驾
public void onSuccess(JSONObject json)抛出JSONException{
//进程响应状态
String status=json.getString(“status”);
如果(!status.equals(“OK”)){
字符串错误=null;
if(status.equals(“未找到”)| | status.equals(“零结果”)){
error=Global.application.getString(R.string.errCantGetDirection);
}否则{
error=Global.application.getString(R.string.errGetDirection);
}
result.instructions=新字符串[]{error};
result.hasrerror=true;
返回;
}
/*
*路由[]支腿[]步骤[]html\U说明
*/
JSONArray arrRoutes=json.getJSONArray(“路由”);
//没有找到路由
if(arrRoutes.length()==0){
result.instructions=newstring[]{Global.application.getString(R.String.errCantGetDirection)};
result.hasrerror=true;
返回;
}
JSONArray arrLegs=arrrouts.getJSONObject(0.getJSONArray(“legs”);
JSONObject firstLeg=arrLegs.getJSONObject(0);
JSONArray arrsteeps=firstLeg.getJSONArray(“步骤”);
int len=arrsteeps.length();
result.instructions=新字符串[len];
result.points=新建LinkedList();
JSONObject leg=null;
//得到指示
对于(int i=0;i>1):(结果>>1));
lat+=dlat;
移位=0;
结果=0;
做{
b=编码的.charAt(索引++)-63;
结果|=(b&0x1f)=0x20);
int-dlng=((结果&1)!=0?~(结果>>1):(结果>>1));
液化天然气+=液化天然气;
地质点p=新的地质点((int)((双)lat/1E5)*1E6),(int)((双)lng/1E5)*1E6));
poly.add(p);
}
返回多边形;
}
}
public class DirectionParser extends JSONParser {
public DirectionResult result = new DirectionResult();
protected int jsonType = JSONParser.GOOGLE_DIRECTION_JSON;
public DirectionParser() {
jsonType = JSONParser.GOOGLE_DIRECTION_JSON;
}
@Override
public void onFailure(String message) {
}
@Override
public void onSuccess(JSONObject json) throws JSONException {
}
}
public class DirectionResult {
public String[] instructions;
public List<GeoPoint> points;
public String duration;
public String distance;
public boolean hasError = false;
}
public类DirectionParser扩展了JSONParser{
public DirectionResult=new DirectionResult();
受保护的int-jsonType=JSONParser.GOOGLE\u DIRECTION\u JSON;
公共方向分析器(){
jsonType=JSONParser.GOOGLE\u DIRECTION\u JSON;
}
@凌驾
公共void onFailure(字符串消息){
}
@凌驾
public void onSuccess(JSONObject json)抛出JSONException{
}
}
公共类定向结果{
公共字符串[]指令;
公共名单点;
公共字符串持续时间;
公共字符串距离;
公共布尔hasrerror=false;
}
instructions = directionResult.instructions;// DirectionResult you got from MapService.
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(DirectionListActivity.this, android.R.layout.simple_list_item_1, instructions);
listDirection.setAdapter(adapter1);
instructions=directionResult.instructions//