2016-08-15 2 views
1

안녕하세요, Google지도 v2에서 직선을 그립니다. 그리고 그 라인에서 모든 LatLng 포인트를 얻으십시오 (참고 :이 라인은 간단하지 않습니다 ... 모든 단계를 얻고 싶습니다).Google지도 v2에서 직선 그리기

Google을 많이 검색했지만 도로 경로를 제공합니다 ... 나는 직선 만 (도로가 아닌)을 원합니다. 이것은 내가 사용하고 코드 :

Polyline line = mMap.addPolyline(new PolylineOptions() 
        .add(origin, dest) 
        .width(10) 
        .color(Color.RED)); 
Log.wtf("Activity","Poits="+line.getPoints()); 

그러나 그것은 기원과의 최종 도착 지점 만 제공합니다! 누구나 모든 점을 얻는 방법을 제안 할 수 있습니까? 고맙습니다!

+0

http://stackoverflow.com/questions/38629519/calculation-for-latitude-and-longitude-from-start-to-destination-point/38629834 – antonio

+0

@antonio에서 고맙습니다! ... 도움을 받았습니다. 나 abit :) –

답변

0

여러 점을 시도해 볼 수 있습니다.

private String getDirectionsUrlForAllPoints(String travelMode, ArrayList<LatLng> points) 
    { 
     //int source, dest = 0; 
     StringBuilder polyLineUrl = new StringBuilder("http://maps.googleapis.com/maps/api/directions/"); 

     // Output format 
     polyLineUrl.append("json?"); 

     StringBuilder wayPointUrl = new StringBuilder(); 

     for (int i = 0; i < points.size(); i++) { 

      if (i == 0) { 
       // Origin of route 
       polyLineUrl.append("origin="); 
       polyLineUrl.append(points.get(i).latitude); 
       polyLineUrl.append(","); 
       polyLineUrl.append(points.get(i).longitude); 
       continue; 
      } 

      // For poly line between last points 
      if (i == points.size() - 1) { 
       //dest = i; 

       // Destination of route 
       polyLineUrl.append("&destination="); 
       polyLineUrl.append(points.get(i).latitude); 
       polyLineUrl.append(","); 
       polyLineUrl.append(points.get(i).longitude); 
       continue; 
      } 

      wayPointUrl.append("|"); 
      wayPointUrl.append(points.get(i).latitude); 
      wayPointUrl.append(","); 
      wayPointUrl.append(points.get(i).longitude); 

      /*https://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true 
      |-34.901374,138.737500|-34.918335,138.720412|-34.919335,138.750412|-34.928335,138.790412|-34.938335,138.800412 
      * */ 


     } 

     // Sensor enabled 
     // Building the parameters to the web service 
     polyLineUrl.append("&sensor=false&optimize=true&mode="); //String parameters = str_origin+"&"+str_dest+"&"+sensor + ; 

     polyLineUrl.append(travelMode); 

     if (wayPointUrl.toString().length() > 0) { 
      wayPointUrl.insert(0, "&waypoints="); 
     } 

     // Building the url to the web service 
     String url = polyLineUrl.toString() + wayPointUrl.toString(); 

     Log.d(TAG, "Url: " + url); 

     return url; 
    } 


    /** 
    * Receives a JSONObject and returns a list of lists containing latitude and longitude 
    */ 
    public List<List<HashMap<String, String>>> parseForAllPoints(JsonObject jObject) { 

     List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String, String>>>(); 
     JsonArray jRoutes = null; 
     JsonArray jLegs = null; 
     JsonArray jSteps = null; 
     JsonObject jDistance = null; 
     JsonObject jDuration = null; 

     try { 


      jRoutes = jObject.getAsJsonArray("routes"); 

      if (jRoutes != null) { 
       /** Traversing all routes */ 
       for (int i = 0; i < jRoutes.size(); i++) { 
        jLegs = ((JsonObject) jRoutes.get(i)).getAsJsonArray("legs"); 

        List<HashMap<String, String>> path = new ArrayList<HashMap<String, String>>(); 

        /** Traversing all legs */ 
        for (int j = 0; j < jLegs.size(); j++) { 

         /** Getting distance from the json data */ 
         jDistance = ((JsonObject) jLegs.get(j)).getAsJsonObject("distance"); 
         HashMap<String, String> hmDistance = new HashMap<String, String>(); 
         hmDistance.put("distance", jDistance.get("text").getAsString()); 

         /** Getting duration from the json data */ 
         jDuration = ((JsonObject) jLegs.get(j)).getAsJsonObject("duration"); 
         HashMap<String, String> hmDuration = new HashMap<String, String>(); 
         hmDuration.put("duration", jDuration.get("text").getAsString()); 

         /** Adding distance object to the path */ 
         path.add(hmDistance); 

         /** Adding duration object to the path */ 
         path.add(hmDuration); 

         jSteps = ((JsonObject) jLegs.get(j)).getAsJsonArray("steps"); 

         /** Traversing all steps */ 
         for (int k = 0; k < jSteps.size(); k++) { 
          String polyline = ""; 
          polyline = ((JsonObject) ((JsonObject) jSteps.get(k)).get("polyline")).get("points").getAsString(); 
          //Log.d(TAG, "parse: " + polyline); 
          List<LatLng> list = decodePoly(polyline); 

          /** Traversing all points */ 
          for (int l = 0; l < list.size(); l++) { 
           HashMap<String, String> hm = new HashMap<String, String>(); 

           if (list.get(l) != null) { 
            hm.put("lat", Double.toString(((LatLng) list.get(l)).latitude)); 
            hm.put("lng", Double.toString(((LatLng) list.get(l)).longitude)); 
            path.add(hm); 
           } 
          } 
         } 
        } 
        routes.add(path); 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return routes; 
    } 


    /** 
    * Method to decode polyline points 
    * Courtesy : jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java 
    */ 
    private List<LatLng> decodePoly(String encoded) { 

     List<LatLng> poly = new ArrayList<LatLng>(); 
     int index = 0, len = encoded.length(); 
     int lat = 0, lng = 0; 

     while (index < len) { 
      int b, shift = 0, result = 0; 
      do { 
       b = encoded.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 = encoded.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b >= 0x20); 
      int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      lng += dlng; 

      LatLng p = new LatLng((((double) lat/1E5)), 
        (((double) lng/1E5))); 
      poly.add(p); 
     } 

     return poly; 
    } 

getDirectionsUrlForAllPoints()에서 URL을받습니다. webservice (get) 호출을 작성한 다음 parseForAllPoints() 메소드를 사용하여 해당 응답을 구문 분석하십시오. 마지막으로 polyline에 latlng의 arraylist를 추가합니다. 모든 점으로 폴리선을 그립니다. 다른 클래스에서이 작업을 수행했지만 여기에 전체 코드를 붙여 넣을 수는 없습니다. 당신이 이해하기를 바랍니다.

+0

이 줄 줄 생각 나게 직선에 ....... 생각? –

+0

죄송합니다. 나는 도로가 아닌 그 부분을 읽지 않았다. 특정 경로에 폴리 라인을 그립니다. 나는 공기 대신 선을 표시하는 것이 아니라 선로를 표시하는 것이 더 낫다고 생각한다. – androidnoobdev

+0

모든 latlng이있는 경우 for 루프를 사용하거나 폴리선 옵션에서 addAll()을 사용하여 점을 추가 한 다음 Google지도에 전달할 수 있습니다. – androidnoobdev