java – 谷歌地图静态地图折线穿过湖泊,河流,山脉
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 谷歌地图静态地图折线穿过湖泊,河流,山脉,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5361字,纯文字阅读大概需要8分钟。
内容图文
![java – 谷歌地图静态地图折线穿过湖泊,河流,山脉](/upload/InfoBanner/zyjiaocheng/706/867d4990e3a64fc5b1128305098c88b1.jpg)
我的程序使用谷歌地图指向网络服务来查找两点之间的路线.结果被解析并存储在变量中.
然后,此变量用于撰写Google静态地图网址.
解析和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的回应:
最终URL如下所示:
解决方法:
您需要在每个步骤的起点和终点之间包含折线路径(它是编码的折线).
从directions web service response:
"steps" : [
{
"distance" : {
"text" : "0.3 km",
"value" : 335
},
"duration" : {
"text" : "1 min",
"value" : 52
},
"end_location" : {
"lat" : -22.9772355,
"lng" : -43.23076390000001
},
"html_instructions" : "Head northeast on R. Marquês de S?o Vicente",
"polyline" : {
"points" : "r}fkCna{fGyBaDMSMSOUMUCMAOOgAEe@Co@?o@?YAWEk@?G"
},
"start_location" : {
"lat" : -22.9783362,
"lng" : -43.2336781
},
"travel_mode" : "DRIVING"
},
// ...
Including all the step polylines (and encoding the polyline) works for me
jsfiddle creating a URL for the static map from the web service response
内容总结
以上是互联网集市为您收集整理的java – 谷歌地图静态地图折线穿过湖泊,河流,山脉全部内容,希望文章能够帮你解决java – 谷歌地图静态地图折线穿过湖泊,河流,山脉所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。