Java 谷歌地图通过湖泊、河流、山脉的静态地图多段线
我的程序使用google maps directions for web服务查找两点之间的路线。结果被解析并存储在变量中。Java 谷歌地图通过湖泊、河流、山脉的静态地图多段线,java,google-maps,google-static-maps,org.json,Java,Google Maps,Google Static Maps,Org.json,我的程序使用google maps directions for web服务查找两点之间的路线。结果被解析并存储在变量中。 然后使用此变量组成google静态地图URL 解析和URL工作正常。问题是绘制的“路线”穿过湖泊和山脉 { String GPS = "-22.978823,-43.233249"; String link = MAPS_BASE_URL + "center=brazil," + GPS + "&markers=color:
然后使用此变量组成google静态地图URL 解析和URL工作正常。问题是绘制的“路线”穿过湖泊和山脉
{
String GPS = "-22.978823,-43.233249";
String link = MAPS_BASE_URL + "center=brazil," + GPS +
"&markers=color:blue|brazil," + GPS +
"&path=color:0xff0000ff" + "%s" +
"&zoom=13&size=1024x1024&sensor=false";
String htmlContent = "";
String direction_URL= "";
URL url = null;
String parsedStr = null;
Scanner scan = null;
origin = GPS;
destination ="Maracanã";
try {
direction_URL = MAPS_DIRECTIONS_URL;
direction_URL += URLEncoder.encode(origin, "UTF-8");
direction_URL += "&destination=";
direction_URL += URLEncoder.encode(destination, "UTF-8");
direction_URL += "&key=AIzaSyARNFl6ns__p2OEy3uCrZMGem8KW8pXwAI";
}catch(UnsupportedEncodingException e){
Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, e);
}
try {
url = new URL(direction_URL);
} catch (MalformedURLException ex) {
Logger.getLogger(AlertService.class.getName()).log(Level.SEVERE, null, ex);
}
try {
scan = new Scanner(url.openStream());
} catch (IOException ex) {
Logger.getLogger(AlertService.class.getName()).log(Level.SEVERE, null, ex);
}
String str = new String();
while (scan.hasNext())
str += scan.nextLine();
scan.close();
parsedStr = parseJson(str);
try {
InputStream htmlInputStream =
AlertService.class.getResourceAsStream("/resources/gapi.html");
BufferedReader htmlReader = new BufferedReader(
new InputStreamReader(htmlInputStream));
String locationsContent = "";
String wilcardContent = "";
Scanner strScanner = new Scanner(parsedStr);
while (strScanner.hasNextLine())
{
locationsContent = strScanner.nextLine() + "\n";
StringTokenizer st = new StringTokenizer(locationsContent, ";");
if (st.countTokens() == 2)
wilcardContent += "|" + st.nextToken().trim()
+ "," + st.nextToken().trim();
}
link = link.replaceFirst("%s", wilcardContent);
htmlContent = "";
while (htmlReader.ready())
htmlContent += htmlReader.readLine() + "\n";
htmlContent = htmlContent.replaceAll("%s", link);
} catch (FileNotFoundException ex) {
Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
}
return htmlContent;
}
解析函数:
private String parseJson(String s){
String coordinates = new String ();
final JSONObject json = new JSONObject(s);
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();
for(int i = 0; i< numSteps; ++i){
final JSONObject step = steps.getJSONObject(i);
final JSONObject startLocation = step.getJSONObject("start_location");
final Double startLat = startLocation.getDouble("lat");
final Double startlng = startLocation.getDouble("lng");
final JSONObject endLocation = step.getJSONObject("end_location");
final Double endtLat = endLocation.getDouble("lat");
final Double endtlng = endLocation.getDouble("lng");
coordinates = coordinates.concat(" ");
coordinates = coordinates.concat(startLat.toString());
coordinates = coordinates.concat(";" + " ");
coordinates = coordinates.concat(startlng.toString());
coordinates = coordinates.concat("\n");
coordinates = coordinates.concat(" ");
coordinates = coordinates.concat(endtLat.toString());
coordinates = coordinates.concat(";" + " ");
coordinates = coordinates.concat(endtlng.toString());
coordinates = coordinates.concat("\n");
}
return coordinates;
}
私有字符串解析JSON(字符串s){
字符串坐标=新字符串();
最终JSONObject json=新的JSONObject;
最终JSONObject jsonRoute=json.getJSONArray(“路由”).getJSONObject(0);
//获取航段,只有一个航段,因为我们不支持航路点
最终JSONObject leg=jsonRoute.getJSONArray(“legs”).getJSONObject(0);
//准备好这条腿的台阶
最终JSONArray步骤=leg.getJSONArray(“步骤”);
//在for循环中使用的步骤数
final int numSteps=steps.length();
对于(int i=0;i
Json响应:
最终URL如下所示:
您需要在每个步骤的起点和终点之间包含多段线路径(它是一条编码的多段线) 从:
我找到了用java解决这个问题的方法。我改编了用户geocozip javascript代码。在我的情况下,由于没有提供航路点,我只需要一条腿。因此,我的解析函数得到如下结果:
List<LatLng> path = new ArrayList();
for(int j = 0; j< numSteps; ++j){
final JSONObject step = steps.getJSONObject(j);
final JSONObject polyline = step.getJSONObject("polyline");
final String polylinePoint = polyline.getString("points");
List<LatLng> coordinates = decodePath(polylinePoint);
for( int k = 0; k < coordinates.size(); ++k){
path.add(coordinates.get(k));
}
}
生成的URL看起来有什么问题?该路由来自哪里?看起来它来自方向服务,但不包括所有的点(只包括每个分支的起点和终点)。@geocodezip我添加了静态地图URL。此外,路线确实来自方向服务。它来自的方向请求/响应是什么样子的?在我看来(正如我在第二次评论中所说的),你只是在回复中包含了一些要点。您可能也想对路径进行编码。@geocodezip您能给我一个如何编辑它的示例吗?您能给我一个如何编辑它的示例吗?为我的答案添加了解析方向服务响应的示例
List<LatLng> path = new ArrayList();
for(int j = 0; j< numSteps; ++j){
final JSONObject step = steps.getJSONObject(j);
final JSONObject polyline = step.getJSONObject("polyline");
final String polylinePoint = polyline.getString("points");
List<LatLng> coordinates = decodePath(polylinePoint);
for( int k = 0; k < coordinates.size(); ++k){
path.add(coordinates.get(k));
}
}
String newPath = path.createPolyLine(encodedPath);
String locationsContent="";
locationsContent = URLEncoder.encode(newPath, "UTF-8")
.replaceAll("\\%40", "@")
.replaceAll("\\+", "%20")
.replaceAll("\\%21", "!")
.replaceAll("\\%27", "'")
.replaceAll("\\%28", "(")
.replaceAll("\\%29", ")");