2017-12-11 9 views
1

내가 원하는 것은 출발지에서 목적지까지 지리적 지점을 얻는 것이고 점 사이의 거리는 1m가 될 수 있습니다. 폴리 라인은 소스와 목적지 사이에 지리적 포인트를 연결하여 만들어집니다. 내가 알지 못하는 것은이 지리적 포인트를 얻을 수 있는가하는 것입니다.Google 길 찾기 api에서 원본에서 대상까지 일련의 지오 포인트를 가져 오는 방법이 있습니까?

+0

문서에서 찾을 수있는 모든 정보 : https://developers.google.com/maps/documentation/directions/intro – Yupi

답변

2

실제로 길 찾기 API에서 경로를 구성하는 LatLng 목록을 얻을 수 있습니다. documentation의 응답 형식을 확인하면 각 경로에 legs[] 배열이 있으며, 차례대로 각 다리의 배열은 steps[]이고 마지막으로 각 단계는 polyline 속성을가집니다. 폴리

서에 따라 스텝의 인코딩 된 폴리 표현을 유지하는 단일 점 객체를 포함한다. 이 폴리 라인은 스텝의 근사 (매끄러운) 경로입니다.

아이디어는 응답을 받고, JSON 개체로 구문 분석하고 다리 및 단계별 경로에 대한 루프를 만듭니다. 단계의 각 폴리 라인 속성을 디코딩하고 결과 LatLng를 목록에 추가해야합니다.

  • 당신이

  • 당신이 인코딩/디코딩을위한 유틸리티 기능이 Google Maps Android API Utility Library 사용할 수 있습니다 encoded polyline 문서에 설명 된 디코딩, 다음의 알고리즘에 대한 자신의 함수를 작성할 수 있습니다

    는 폴리 라인 디코딩하는 몇 가지 옵션이 있습니다 폴리 라인

  • 마지막으로 인코딩 된/디코딩 된 폴리 라인 알고리즘이 구현 된 Java client library for Google Maps Web services을 사용할 수 있습니다.

Google Maps 웹 서비스 용 Java 클라이언트 라이브러리가 아마도이를 구현하는 가장 쉬운 방법 일 것입니다. 코드 조각은

//Define list to get all latlng for the route 
List<LatLng> path = new ArrayList(); 

//Execute Directions API request 
GeoApiContext context = new GeoApiContext.Builder() 
     .apiKey("YOUR_API_KEY") 
     .build(); 
DirectionsApiRequest req = DirectionsApi.getDirections(context, "41.385064,2.173403", "40.416775,-3.70379"); 
try { 
    DirectionsResult res = req.await(); 

    //Loop through legs and steps to get encoded polylines of each step 
    if (res.routes != null && res.routes.length > 0) { 
     DirectionsRoute route = res.routes[0]; 

     if (route.legs !=null) { 
      for(int i=0; i<route.legs.length; i++) { 
       DirectionsLeg leg = route.legs[i]; 
       if (leg.steps != null) { 
        for (int j=0; j<leg.steps.length;j++){ 
         DirectionsStep step = leg.steps[j]; 
         if (step.steps != null && step.steps.length >0) { 
          for (int k=0; k<step.steps.length;k++){ 
           DirectionsStep step1 = step.steps[k]; 
           EncodedPolyline points1 = step1.polyline; 
           if (points1 != null) { 
            //Decode polyline and add points to list of route coordinates 
            List<com.google.maps.model.LatLng> coords1 = points1.decodePath(); 
            for (com.google.maps.model.LatLng coord1 : coords1) { 
             path.add(new LatLng(coord1.lat, coord1.lng)); 
            } 
           } 
          } 
         } else { 
          EncodedPolyline points = step.polyline; 
          if (points != null) { 
           //Decode polyline and add points to list of route coordinates 
           List<com.google.maps.model.LatLng> coords = points.decodePath(); 
           for (com.google.maps.model.LatLng coord : coords) { 
            path.add(new LatLng(coord.lat, coord.lng)); 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} catch(Exception ex) { 
    Log.e(TAG, ex.getLocalizedMessage()); 
} 

//Draw the polyline 
if (path.size() > 0) { 
    PolylineOptions opts = new PolylineOptions().addAll(path).color(Color.BLUE).width(5); 
    mMap.addPolyline(opts); 
} 

다음 당신은 당신과 함께 API 키를 대체 잊지 마세요 https://github.com/xomena-so/so47492459

에서 전체 샘플 프로젝트를 다운로드 할 수있는 수 있습니다.

도움이 되었기를 바랍니다.

+0

감사합니다. 톤을 얻는 것이 가장 쉽습니다. –