谷歌地图通过湖泊,河流,山脉的静态地图折线 [英] google maps static map polyline passing through lakes, river, mountains

查看:125
本文介绍了谷歌地图通过湖泊,河流,山脉的静态地图折线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序使用Google地图服务的Google地图方向来查找两点之间的路线。结果被解析并存储在变量中。

此变量用于编写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;
字符串parsedStr = null;
扫描仪扫描=空;

origin = GPS;
destination =Maracanã;

尝试{
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);
}

尝试{
url =新网址(direction_URL);
} catch(MalformedURLException ex){
Logger.getLogger(AlertService.class.getName()).log(Level.SEVERE,null,ex);
}

尝试{
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);

尝试{
InputStream htmlInputStream =
AlertService.class.getResourceAsStream(/ resources / gapi.html);

BufferedReader htmlReader = new BufferedReader(
New InputStreamReader(htmlInputStream));

字符串locationsContent =;
String wilcardContent =;

扫描仪strScanner =新扫描仪(parsedStr); (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 =; (htmlReader.ready())
htmlContent + = htmlReader.readLine()+\\\
;
while

htmlContent = htmlContent.replaceAll(%s,link);
$ b $ 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);
}
返回htmlContent;



$ b $解析函数:

$ b pre> private String parseJson(String s){
String coordinates = new String();
final JSONObject json = new JSONObject(s);
final JSONObject jsonRoute = json.getJSONArray(routes)。getJSONObject(0);
//获取腿,只有一条腿,因为我们不支持路标
final JSONObject leg = jsonRoute.getJSONArray(legs)。getJSONObject(0);
//获得这一步的步骤
final JSONArray steps = leg.getJSONArray(steps);
//在for循环中使用的步数
final int numSteps = steps.length();

for(int i = 0; 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(\\\
);

coordinates = coordinates.concat();
coordinates = coordinates.concat(endtLat.toString());
coordinates = coordinates.concat(;+);
coordinates = coordinates.concat(endtlng.toString());
coordinates = coordinates.concat(\\\
);
}
返回坐标;
}

Json回应:

https://maps.googleapis.com/maps/api/directions/json?origin=-22.978823,-43.233249&destination=Maracan%C3%A2&key=AIzaSyARNFl6ns__p2OEy3uCrZMGem8KW8pXwAI



最终到达网址如下所示:



http://maps.google.com/maps/api/staticmap?center=brazil,-22.978823,-43.233249&markers=color:blue|brazil,-22.978823,- 43.233249&安培;路径=彩色:0xff0000ff | -22.9783362,-43.2336781 | -22.9772355,-43.23076390000001 | -22.9772355,-43.23076390000001 | -22.9789792,-43.2300162 | -22.9789792,-43.2300162 | -22.9790772,-43.23036 | -22.9790772,-43.23036 | -22.9786979,-43.22698949999999 | -22.9786979,-43.22698949999999 | -22.9771399,-43.2196208 | -22.9771399,-43.2196208 | -22.9624962,-43.20396840000001 | -22.9624962,-43.20396840000001 | -22.9583858,-43.2043807 | -22.9583858,-43.2043807 | - 22.934896,-43.2 094454 | -22.934896,-43.2094454 | -22.9333061,-43.2096873 | -22.9333061,-43.2096873 | -22.913577,-43.2099889 | -22.913577,-43.2099889 | -22.9106681,-43.2154625 | -22.9106681,-43.2154625 | -22.9101261,-43.2217923 | -22.9101261,-43.2217923 | -22.9114561,-43.2254838 | -22.9114561,-43.2254838 | -22.9135546,-43.2276606& zoom = 13& size = 1024x1024& sensor = false



p>从路线网络服务响应

 steps:[
{
distance :{
text:0.3 km,
value:335
},
duration:{
text:1分钟,
值:52
},
end_location:{
lat:-22.9772355,
lng:-43.23076390000001
},
html_instructions:Head \\\northeast\ \\\R上的u003c / b \\\>。 MarquêsdeSãoVicente'u003c / b\\\>,
polyline:{
points:r} fkCna {fGyBaDMSMSOUMUCMAOOgAEe @ Co @?o @?YAWEk @?G
},
start_location:{
lat:-22.9783362,
lng:-43.2336781
},
travel_mode: DRIVING
},
// ...

包括所有步骤折线(以及编码折线)

jsfiddle从网络服务响应中为静态地图创建一个网址




My program uses google maps directions for web Services to find a route between two points. The result is parsed and stored in variable.
This variable is then used to compose google static map URL.

The parse and the URL are working correctly. The problem is that the drawn "route" passes through a lake and mountains.

{
    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;
}

Parse function:

 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 response:

https://maps.googleapis.com/maps/api/directions/json?origin=-22.978823,-43.233249&destination=Maracan%C3%A2&key=AIzaSyARNFl6ns__p2OEy3uCrZMGem8KW8pXwAI

The Final URL look like this:

http://maps.google.com/maps/api/staticmap?center=brazil,-22.978823,-43.233249&markers=color:blue|brazil,-22.978823,-43.233249&path=color:0xff0000ff|-22.9783362,-43.2336781|-22.9772355,-43.23076390000001|-22.9772355,-43.23076390000001|-22.9789792,-43.2300162|-22.9789792,-43.2300162|-22.9790772,-43.23036|-22.9790772,-43.23036|-22.9786979,-43.22698949999999|-22.9786979,-43.22698949999999|-22.9771399,-43.2196208|-22.9771399,-43.2196208|-22.9624962,-43.20396840000001|-22.9624962,-43.20396840000001|-22.9583858,-43.2043807|-22.9583858,-43.2043807|-22.934896,-43.2094454|-22.934896,-43.2094454|-22.9333061,-43.2096873|-22.9333061,-43.2096873|-22.913577,-43.2099889|-22.913577,-43.2099889|-22.9106681,-43.2154625|-22.9106681,-43.2154625|-22.9101261,-43.2217923|-22.9101261,-43.2217923|-22.9114561,-43.2254838|-22.9114561,-43.2254838|-22.9135546,-43.2276606&zoom=13&size=1024x1024&sensor=false

解决方案

You need to include the polyline path between each step's start and end points (it is an encoded polyline).

From the 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 \u003cb\u003enortheast\u003c/b\u003e on \u003cb\u003eR. Marquês de São Vicente\u003c/b\u003e",
       "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

这篇关于谷歌地图通过湖泊,河流,山脉的静态地图折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆