2012-11-03 3 views
1

Python을 사용하여 Directions API를 사용하고 있습니다. Python에서 Google Maps Direction API의 JSON 구문 분석

mapApi = GoogleMaps(API_KEY) 
directionsObj.routes = mapApi.directions(directionsObj.source, directionsObj.destination) 
print "directionsObj.routesssss", directionsObj.routes['routes'] 

나는 길 찾기 API의 JSON 응답 파일을 얻기 위해 위의 코드를 사용하고 난 (데이터베이스 사용) mongoengine하기 위해 JSON을 저장. 이제 터미널에서 위의 코드를 실행할 때 다음과 같은 결과가 나타납니다.

JSON 파일 : 나는 단지 start_address, end_address, u'html_instructions에만 얻을 수 있도록 위의 JSON 파일을 구문 분석하는 방법

[{u'overview_polyline': {u'points': u'yiv{@[email protected]@[email protected]@}[email protected]}[email protected]@pEpBbLlBnItCxMlBlHdBpGzB|[email protected]@`@`[email protected]{[email protected]][email protected]^Q^[email protected]`@[[email protected]@[email protected]@{IhD{[email protected]@[email protected]\\kAxByErKeB|[email protected]@[[email protected]@JgD\\[email protected]@}@[email protected]@[email protected]@[email protected]@[email protected][email protected]@hAmCHeDDkAFsBJkI`@_BJyG\\s^bB}QvAa\\[email protected][email protected]@bBDVCJ?XL|@X`[email protected]@^[email protected]`@[email protected]|@F^[email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@\\{[email protected]}[email protected]@[email protected]`@[email protected]@[email protected]{@[email protected]`@oB`@[email protected][email protected]^[email protected]^}@[email protected]@EDaD^[email protected]@bC\\tBVxB\\[email protected]\\[email protected]@[email protected]'}, u'copyrights': u'Map data \xa92012 Google', u'warnings': [], u'legs': [{u'distance': {u'text': u'11.1 km', u'value': 11118}, u'end_address': u'Kaloor, Ernakulam, Kerala, India', u'via_waypoint': [], u'start_address': u'Thrippunithura, Kerala, India', u'duration': {u'text': u'19 mins', u'value': 1126}, u'steps': [{u'html_instructions': u'Head west', u'distance': {u'text': u'33 m', u'value': 33}, u'polyline': {u'points': u'yiv{@[email protected]'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.949890000000002, u'lng': 76.34929000000001}, u'duration': {u'text': u'1 min', u'value': 4}, u'end_location': {u'lat': 9.949800000000003, u'lng': 76.34901}}, {u'html_instructions': u'Turn right toward Ernakulam-Ettumanoor Rd/SH 15', u'distance': {u'text': u'8 m', u'value': 8}, u'polyline': {u'points': u'giv{@i|~pMKD'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.949800000000003, u'lng': 76.34901}, u'duration': {u'text': u'1 min', u'value': 11}, u'end_location': {u'lat': 9.949860000000001, u'lng': 76.34898000000001}}, {u'html_instructions': u'Take the 1st left toward Ernakulam-Ettumanoor Rd/SH 15', u'distance': {u'text': u'0.2 km', u'value': 211}, u'polyline': {u'points': u'siv{@c|[email protected]\\D\\RjA^|BL|@'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.949860000000001, u'lng': 76.34898000000001}, u'duration': {u'text': u'1 min', u'value': 28}, u'end_location': {u'lat': 9.949400000000002, u'lng': 76.34711}}, {u'html_instructions': u'Turn right onto Ernakulam-Ettumanoor Rd/SH 15Pass by Abhayam (on the right in 400m)', u'distance': {u'text': u'0.6 km', u'value': 608}, u'polyline': {u'points': u'wfv{@mp~pMcA\\[email protected][[email protected][[email protected]@[email protected]{@V}@V}[email protected]@T]JiDhA'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.949400000000002, u'lng': 76.34711}, u'duration': {u'text': u'1 min', u'value': 71}, u'end_location': {u'lat': 9.95439, u'lng': 76.34486000000001}}, {u'html_instructions': u'Turn left at SN Jct onto Kochi - Madurai - Tondi Point Hwy/Kochi - Madurai - Tondi Point Highway/NH 85Pass by ICICI Bank (on the left)', u'distance': {u'text': u'1.5 km', u'value': 1536}, u'polyline': {u'points': u'}ew{@[email protected]^[email protected]`[email protected]@[email protected]@[email protected]@[email protected]@vCT|@[email protected]|[email protected][email protected]@`DJ\\[email protected]^rAL`@JXL\\[email protected]'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.95439, u'lng': 76.34486000000001}, u'duration': {u'text': u'2 mins', u'value': 112}, u'end_location': {u'lat': 9.949900000000001, u'lng': 76.33162}}, {u'html_instructions': u'Turn right at Petta Jct onto Ernakulam Ettumanoor Rd', u'distance': {u'text': u'54 m', u'value': 54}, u'polyline': {u'points': u'{iv{@so{[email protected]^[email protected]'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.949900000000001, u'lng': 76.33162}, u'duration': {u'text': u'1 min', u'value': 59}, u'end_location': {u'lat': 9.9503, u'lng': 76.33136}}, {u'html_instructions': u'Continue onto SH 15 Ernakulam -Ettumanoor HwyPass by Reliance Petrol Pump (on the right in 1.5km)', u'distance': {u'text': u'2.5 km', u'value': 2516}, u'polyline': {u'points': u'klv{@_n{[email protected]@`@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XaEzA{[email protected]@[email protected]^}@[email protected]{A|@WNMHYPQJKHKHCBCDEFKP][email protected][[email protected]@dAQ\\[email protected]`[email protected]@dAO\\[email protected]@[email protected]`@[email protected]@[XONKHOHSDQDWF[[email protected]@[email protected]?[A][email protected]][email protected]@[email protected]@YDYFWH[J'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.9503, u'lng': 76.33136}, u'duration': {u'text': u'3 mins', u'value': 153}, u'end_location': {u'lat': 9.967500000000001, u'lng': 76.31912000000001}}, {u'html_instructions': u'Continue onto Sahodaran Ayyappan Rd', u'distance': {u'text': u'0.1 km', u'value': 134}, u'polyline': {u'points': u'{wy{@[email protected]@IR'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.967500000000001, u'lng': 76.31912000000001}, u'duration': {u'text': u'1 min', u'value': 59}, u'end_location': {u'lat': 9.968240000000002, u'lng': 76.31817000000001}}, {u'html_instructions': u'Turn right at Vyttila Jct onto NH 66Pass by Muthoot Motors (on the left in 1.6km)', u'distance': {u'text': u'2.8 km', u'value': 2771}, u'polyline': {u'points': u'o|y{@q{[email protected]@[email protected][email protected]@[email protected]@[email protected]}ALwCTmNpA{D\\[email protected]@[email protected]@[email protected]@F'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.968240000000002, u'lng': 76.31817000000001}, u'duration': {u'text': u'3 mins', u'value': 205}, u'end_location': {u'lat': 9.99295, u'lng': 76.31493}}, {u'html_instructions': u'Turn left toward NH66 Service Road', u'distance': {u'text': u'9 m', u'value': 9}, u'polyline': {u'points': u'}v~{@[email protected]'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.99295, u'lng': 76.31493}, u'duration': {u'text': u'1 min', u'value': 11}, u'end_location': {u'lat': 9.992930000000001, u'lng': 76.31485}}, {u'html_instructions': u'Turn left onto NH66 Service Road', u'distance': {u'text': u'24 m', u'value': 24}, u'polyline': {u'points': u'yv~{@[email protected]?'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.992930000000001, u'lng': 76.31485}, u'duration': {u'text': u'1 min', u'value': 11}, u'end_location': {u'lat': 9.99272, u'lng': 76.31488}}, {u'html_instructions': u'Take the 1st right onto Puthiya Rd', u'distance': {u'text': u'0.6 km', u'value': 621}, u'polyline': {u'points': u'ou~{@[email protected]@[email protected][email protected]@[email protected][email protected]@[email protected]@@[email protected]^LRRXLZFPDJDLDPBLBPB\\@[email protected]@[email protected]@[email protected]@[email protected][email protected]@[email protected]'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.99272, u'lng': 76.31488}, u'duration': {u'text': u'1 min', u'value': 51}, u'end_location': {u'lat': 9.990900000000002, u'lng': 76.30962000000001}}, {u'html_instructions': u'Turn right at Puthiya Road Junction onto Thammanam RdPass by Thammanam Mosque (on the left)', u'distance': {u'text': u'0.6 km', u'value': 623}, u'polyline': {u'points': u'cj~{@[email protected]@RQHWH[[email protected]@[email protected]@[email protected]?S?O?OAQC]E][email protected]@[email protected][email protected][email protected]@S?'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.990900000000002, u'lng': 76.30962000000001}, u'duration': {u'text': u'2 mins', u'value': 98}, u'end_location': {u'lat': 9.996120000000001, u'lng': 76.30793000000001}}, {u'html_instructions': u'Continue onto Mahakavi Vailoppilli Rd', u'distance': {u'text': u'0.2 km', u'value': 187}, u'polyline': {u'points': u'wj_|@q{[email protected]@[email protected][[email protected]@DOB'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.996120000000001, u'lng': 76.30793000000001}, u'duration': {u'text': u'1 min', u'value': 42}, u'end_location': {u'lat': 9.99779, u'lng': 76.30775000000003}}, {u'html_instructions': u'Turn left onto Indira Road', u'distance': {u'text': u'0.3 km', u'value': 343}, u'polyline': {u'points': u'eu_|@[email protected]^[email protected]@[email protected]'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.99779, u'lng': 76.30775000000003}, u'duration': {u'text': u'1 min', u'value': 45}, u'end_location': {u'lat': 9.996540000000001, u'lng': 76.30512}}, {u'html_instructions': u'Turn right onto St. Martin Road', u'distance': {u'text': u'0.6 km', u'value': 641}, u'polyline': {u'points': u'km_|@[email protected]@[email protected]@[email protected][[email protected]@[email protected][email protected]^'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 9.996540000000001, u'lng': 76.30512}, u'duration': {u'text': u'1 min', u'value': 89}, u'end_location': {u'lat': 10.00178, u'lng': 76.30334}}, {u'html_instructions': u'Turn left at Worldwide IPR Services onto NH 47Pass by Juma Masjid (on the left)Destination will be on the left', u'distance': {u'text': u'0.8 km', u'value': 799}, u'polyline': {u'points': u'cn`|@{[email protected]@BRX`BRxAB^D`@[email protected]^[email protected]@[email protected]@HXHXN\\`@[email protected]|@[email protected][email protected]'}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 10.00178, u'lng': 76.30334}, u'duration': {u'text': u'1 min', u'value': 77}, u'end_location': {u'lat': 9.998700000000001, u'lng': 76.29695000000001}}], u'start_location': {u'lat': 9.949890000000002, u'lng': 76.34929000000001}, u'end_location': {u'lat': 9.998700000000001, u'lng': 76.29695000000001}}], u'bounds': {u'northeast': {u'lat': 10.00178, u'lng': 76.34929000000001}, u'southwest': {u'lat': 9.949400000000002, u'lng': 76.29695000000001}}, u'waypoint_order': [], u'summary': u'NH 66'}] 

.

for leg in directionsObj.route['legs']: 
    startAddress = leg['start_address'] 
    print "Start Address:", startAddress 
    endAddress = leg['end_address'] 
    print "End Address:", endAddress 
    for step in leg['steps']: 
     html_instructions = step['html_instructions'] 
     print "html instructions", html_instructions 
+0

Thid는 Google지도 API 3에 관한 것이 아닙니다. – Marcelo

+0

죄송합니다. 그리고 고마워요. – ToothLess

+0

당신은 환영합니다. "선생님"이라고 부를 필요는 없습니다 .-) – Marcelo

답변

0

이 코드는 start_address, end_address, html_instructions을에 geting 일했다. 사용자 환경에 googlemaps 모듈을 설치하고 Google 개발자 콘솔에 SERVER Key를 등록하고 Google Directions API를 사용하도록 설정하십시오.

from googlemaps import Client 

    # Add you API key here 
    mapService = Client(key='api_key') 

    directions = mapService.directions('start', 'destination') 
    directions = directions[0] 

    i=1 
    for leg in directions['legs']: 
     startAddress = leg['start_address'] 
     print "Start Address:", startAddress 
     endAddress = leg['end_address'] 
     print "End Address:", endAddress 
     for step in leg['steps']: 
      html_instructions = step['html_instructions'] 
      print "STEP {} {}".format(i ,html_instructions) 
      i = i+1 
0

근무 파이썬 스크립트 :