显示路由路径名 [英] displaying route path names
本文介绍了显示路由路径名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这个类//主要活动在注释中显示如下两个错误
进口android.graphics.Color;
进口android.os.Bundle;进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapView;公共类NewroutepathActivity扩展MapActivity {
/ **当第一次创建活动调用。 * /
MapView类MapView类;
私人字符串US_API; @覆盖 公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//图形页面=(图形页面)findViewById(R.id.mapview);
MapView类MapView类=(图形页面)findViewById(R.id.mapview); //或者你可以用API密钥直接宣告它
路线路线=方向(新的GeoPoint((int)的(26.2 * 1E6),(INT)(50.6 * 1E6)),新的GeoPoint((int)的(26.3 * 1E6),(INT)(50.7 * 1E6)));//将obove线也越来越错误
RouteOverlay routeOverlay =新RouteOverlay(路线,Color.BLUE);
调用MapView.getOverlays()加(routeOverlay)。 } @覆盖
保护布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
私人路线指引(最终的GeoPoint开始,最终的GeoPoint DEST){
分析器分析器;
字符串jsonURL =maps.google.com/maps/api/directions/json?
最后的StringBuffer SBUF =新的StringBuffer(jsonURL);
sBuf.append(原产地=);
sBuf.append(start.getLatitudeE6()/ 1E6);
sBuf.append(,);
sBuf.append(start.getLongitudeE6()/ 1E6);
sBuf.append(&放大器;目的地=);
sBuf.append(dest.getLatitudeE6()/ 1E6);
sBuf.append(,);
sBuf.append(dest.getLongitudeE6()/ 1E6);
sBuf.append(与&传感器=真放;模式=驾驶);
解析器=新GoogleParser(sBuf.toString());
路线R = parser.parse(); //这里得到错误
返回ř;
}
}
这里
googleparser类有一个错误
进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;
进口的java.util.List;进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.util.Log;进口com.google.android.maps.GeoPoint;公共类GoogleParser XMLParser的扩展实现解析器{
/ **覆盖距离。 ** /
私人诠释距离; 公共GoogleParser(字符串feedUrl){
超(feedUrl);
} / **
*解析一个URL指向谷歌JSON对象到路由对象。
返回:基于JSON对象的Route对象。
* / 大众路线解析(){
//转流成字符串
最后的字符串结果= convertStreamToString(this.getInputStream());//以上线得到一个错误//创建一个空的路线
最终路线路线=新干线();
//创建一个空段
最后一段段=新段();
尝试{
//变换分析串入一个JSON对象
最后JSONObject的JSON =新的JSONObject(结果);
//获取路由对象
最终的JSONObject jsonRoute = json.getJSONArray(路线)getJSONObject(0);
//获取一条腿,只有一条腿,因为我们不支持航点
最终的JSONObject腿= jsonRoute.getJSONArray(腿)getJSONObject(0);
//获取这条腿的步骤
最后JSONArray步骤= leg.getJSONArray(步);
//对于在循环使用的步数
最终诠释numSteps = steps.length();
//设置使用开始和放大器的这条路线的名称;结束地址
route.setName(leg.getString(START_ADDRESS)+至+ leg.getString(END_ADDRESS));
//获取谷歌的版权声明(TOS要求)
route.setCopyright(jsonRoute.getString(版权));
//获取路线的总长度。
route.setLength(leg.getJSONObject(距离)调用getInt(值));
//获取任何警告提供(TOS要求)
如果(!jsonRoute.getJSONArray(警告)。ISNULL(0)){
route.setWarning(jsonRoute.getJSONArray(警告)的getString(0));
}
/ *完成相关步骤循环,从而为每一个和一个段
*解码发现任何折线,因为我们去添加到路由对象的
*地图数组。使用一个明确的循环,因为它的速度更快!
* /
的for(int i = 0; I< numSteps;我++){
//获取单独步骤
最后的JSONObject步骤= steps.getJSONObject(ⅰ);
//获取此步骤的开始位置,并设置上段
最终的JSONObject开始= step.getJSONObject(START_LOCATION);
最终的GeoPoint位置=新的GeoPoint((INT)(start.getDouble(纬度)* 1E6)
(INT)(start.getDouble(LNG)* 1E6));
segment.setPoint(位置);
//设置该段米的长度
最终诠释长度= step.getJSONObject(距离)调用getInt(值)。
距离+ =长度;
segment.setLength(长);
segment.setDistance(距离/ 1000);
从谷歌的方向//删除HTML和设置转向指令
segment.setInstruction(step.getString(html_instructions)的replaceAll(≤(*)*方式>?,));
//检索和放大器;德code这部分的折线,并将其添加到路由。
route.addPoints(德codePolyLine(step.getJSONObject(折线)的getString(分)));
//推段的副本路线
route.addSegment(segment.copy());
}
}赶上(JSONException E){
Log.e(e.getMessage(),谷歌JSON解析器 - + feedUrl);
}
返回路线;
} / **
*转换的InputStream为字符串。
* @参数输入的InputStream转换。
返回:InputStream中的字符串。
* / 私人静态字符串convertStreamToString(最终的InputStream输入){
最终的BufferedReader读者=新的BufferedReader(新的InputStreamReader(输入));
最后StringBuilder的SBUF =新的StringBuilder(); 串线= NULL;
尝试{
而((行= reader.readLine())!= NULL){
sBuf.append(线);
}
}赶上(IOException异常五){
Log.e(e.getMessage(),谷歌解析器stream2string);
} {最后
尝试{
input.close();
}赶上(IOException异常五){
Log.e(e.getMessage(),谷歌解析器stream2string);
}
}
返回sBuf.toString();
} / **
*德code折线串入GeoPoints的列表。
* @参数聚折线连接codeD字符串脱code。
* @返回GeoPoints名单重新受此polystring psented $ P $。
* / 私人列表<&GeoPoint的GT;德codePolyLine(最后弦乐聚){
INT LEN = poly.length();
INT索引= 0;
清单<&GeoPoint的GT;德codeD =新的ArrayList<&GeoPoint的GT;();
INT纬度= 0;
INT LNG = 0; 而(指数< LEN){
INT B:
INT移位= 0;
INT结果为0;
做{
B = poly.charAt(指数++) - 63;
结果| =(B&安培; 0x1F的)LT;<转移;
移动+ = 5;
}而(B> = 0x20的);
INT DLAT =((导致与放大器; 1)= 0〜(结果>→1):?(导致与GT;→1));
纬度+ = DLAT; 移= 0;
结果= 0;
做{
B = poly.charAt(指数++) - 63;
结果| =(B&安培; 0x1F的)LT;<转移;
移动+ = 5;
}而(B> = 0x20的);
INT DLNG =((导致与放大器; 1)= 0〜(结果>→1):?(导致与GT;→1));
LNG + = DLNG; 德coded.add(新的GeoPoint(
(中间体)(纬度* 1E6 / 1E5),(INT)(LNG * 1E6 / 1E5)));
} 返回德codeD;
}
}
解决方案
这是发生在我身上了。为了解决这个问题 - 在函数'方向',当你开始构建jsonURL字符串,添加的http://开头,之前只是'map.google.com ......
。//main activity in this class two errors shown below within the comments
import android.graphics.Color;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class NewroutepathActivity extends MapActivity {
/** Called when the activity is first created. */
MapView mapView;
private String US_API;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//mapView = (MapView) findViewById(R.id.mapview);
MapView mapView = (MapView) findViewById(R.id.mapview); //or you can declare it directly with the API key
Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));
//the obove line also getting error
RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
mapView.getOverlays().add(routeOverlay);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private Route directions(final GeoPoint start, final GeoPoint dest) {
Parser parser;
String jsonURL = "maps.google.com/maps/api/directions/json?";
final StringBuffer sBuf = new StringBuffer(jsonURL);
sBuf.append("origin=");
sBuf.append(start.getLatitudeE6()/1E6);
sBuf.append(',');
sBuf.append(start.getLongitudeE6()/1E6);
sBuf.append("&destination=");
sBuf.append(dest.getLatitudeE6()/1E6);
sBuf.append(',');
sBuf.append(dest.getLongitudeE6()/1E6);
sBuf.append("&sensor=true&mode=driving");
parser = new GoogleParser(sBuf.toString());
Route r = parser.parse();// here getting error
return r;
}
}
googleparser classes here one error
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import com.google.android.maps.GeoPoint;
public class GoogleParser extends XMLParser implements Parser {
/** Distance covered. **/
private int distance;
public GoogleParser(String feedUrl) {
super(feedUrl);
}
/**
* Parses a url pointing to a Google JSON object to a Route object.
* @return a Route object based on the JSON object.
*/
public Route parse() {
// turn the stream into a string
final String result = convertStreamToString(this.getInputStream());
//The above line is getting one error
//Create an empty route
final Route route = new Route();
//Create an empty segment
final Segment segment = new Segment();
try {
//Tranform the string into a json object
final JSONObject json = new JSONObject(result);
//Get the route object
final JSONObject jsonRoute = json.getJSONArray("routes").getJSONObject(0);
//Get the leg, only one leg as we don't support waypoints
final JSONObject leg = jsonRoute.getJSONArray("legs").getJSONObject(0);
//Get the steps for this leg
final JSONArray steps = leg.getJSONArray("steps");
//Number of steps for use in for loop
final int numSteps = steps.length();
//Set the name of this route using the start & end addresses
route.setName(leg.getString("start_address") + " to " + leg.getString("end_address"));
//Get google's copyright notice (tos requirement)
route.setCopyright(jsonRoute.getString("copyrights"));
//Get the total length of the route.
route.setLength(leg.getJSONObject("distance").getInt("value"));
//Get any warnings provided (tos requirement)
if (!jsonRoute.getJSONArray("warnings").isNull(0)) {
route.setWarning(jsonRoute.getJSONArray("warnings").getString(0));
}
/* Loop through the steps, creating a segment for each one and
* decoding any polylines found as we go to add to the route object's
* map array. Using an explicit for loop because it is faster!
*/
for (int i = 0; i < numSteps; i++) {
//Get the individual step
final JSONObject step = steps.getJSONObject(i);
//Get the start position for this step and set it on the segment
final JSONObject start = step.getJSONObject("start_location");
final GeoPoint position = new GeoPoint((int) (start.getDouble("lat")*1E6),
(int) (start.getDouble("lng")*1E6));
segment.setPoint(position);
//Set the length of this segment in metres
final int length = step.getJSONObject("distance").getInt("value");
distance += length;
segment.setLength(length);
segment.setDistance(distance/1000);
//Strip html from google directions and set as turn instruction
segment.setInstruction(step.getString("html_instructions").replaceAll("<(.*?)*>", ""));
//Retrieve & decode this segment's polyline and add it to the route.
route.addPoints(decodePolyLine(step.getJSONObject("polyline").getString("points")));
//Push a copy of the segment to the route
route.addSegment(segment.copy());
}
} catch (JSONException e) {
Log.e(e.getMessage(), "Google JSON Parser - " + feedUrl);
}
return route;
}
/**
* Convert an inputstream to a string.
* @param input inputstream to convert.
* @return a String of the inputstream.
*/
private static String convertStreamToString(final InputStream input) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(input));
final StringBuilder sBuf = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sBuf.append(line);
}
} catch (IOException e) {
Log.e(e.getMessage(), "Google parser, stream2string");
} finally {
try {
input.close();
} catch (IOException e) {
Log.e(e.getMessage(), "Google parser, stream2string");
}
}
return sBuf.toString();
}
/**
* Decode a polyline string into a list of GeoPoints.
* @param poly polyline encoded string to decode.
* @return the list of GeoPoints represented by this polystring.
*/
private List<GeoPoint> decodePolyLine(final String poly) {
int len = poly.length();
int index = 0;
List<GeoPoint> decoded = new ArrayList<GeoPoint>();
int lat = 0;
int lng = 0;
while (index < len) {
int b;
int shift = 0;
int result = 0;
do {
b = poly.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 = poly.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
decoded.add(new GeoPoint(
(int) (lat*1E6 / 1E5), (int) (lng*1E6 / 1E5)));
}
return decoded;
}
}
解决方案
That happened to me too. To solve it - in function 'direction', when you start to build the jsonURL string, add 'http://' at the beginning, just before 'map.google.com...'.
这篇关于显示路由路径名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文