当前位置: 首页 > 面试题库 >

谷歌地图静态地图折线穿过湖泊,河流,山脉

钱星华
2023-03-14
问题内容

我的程序使用Google Maps的网络服务路线来查找两点之间的路线。结果被解析并存储在变量中。
然后,此变量用于组成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;     
}

杰森回应:

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
|巴西,-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


问题答案:

我想出了如何在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格式输入。

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", ")");


 类似资料:
  • 我试图找到android maps api v2的方法,它将决定我在移动时创建的折线的长度。我会把它放在onLocationChanged()中,以便不断更新。有人知道方法是什么吗?maps api将显示哪些单元的长度吗?

  • 我有一些谷歌地图折线。我试图在它们周围画两条折线,这样它们就形成了某种边界: 所以对于原始折线的每一个点,我计算边界线点,从原点算起25米: 在这个例子中,让我们省略折线的第一个和最后一个点。所以我总是看上一个点和下一个点,计算中心点的偏移量。

  • 所以我看到了一个特殊的例外,我敢肯定的是谷歌地图的绘图代码。 我有一个片段,在那里我以编程方式添加了一个支持地图片段,然后我在其中操纵谷歌地图实例。 这是stacktrace: 我无法可靠地复制它(尽管这种情况经常发生),我已经查看了ReadWriteDirectByteBuffer和ShortToByteBufferAdapter,但没有任何东西突然出现在我面前。 有什么想法吗?

  • 我认为问题是我需要添加构建提示。我可以找到在哪里添加他们,但我不确定他们应该是什么格式。我也不知道要补充什么。我想我发现了一个帖子,说我需要添加Android.xapplication=Android:value=“Your Key”/>Android.xPermissions= 但我不确定构建提示条目表单中的格式是什么

  •        LSV默认的对谷歌影像进行了加载,如果需要加载其他的谷歌地图数据,可以通过LSV中直接点击即可加载。

  • 我正在从导入geoJSON数据。jsons文件。它在某些文件上工作得很好,但在其他文件上就不行了。我认为我的文件有问题,但我在geojson lint和geojson.io上测试了它们,没有问题。 这是堆栈: 这是我非常简单的代码 这是我的一个JSON导致了这个错误 build.gradle(项目)和build.gradle(应用程序) 欢迎任何想法