2012-11-03 21 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響應文件,我所存儲的JSON來mongoengine(所使用的數據庫)。現在在終端中,當我運行上面的代碼時,我得到以下結果。

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'}] 

如何分析上面的JSON文件,這樣我可以得到的只有只有start_addressend_addressu'html_instructions

+0

這不是谷歌地圖API 3. – Marcelo

+0

對不起,先生。謝謝你糾正 – ToothLess

+0

不客氣,你不需要打電話給我「先生」;-) – Marcelo

回答

0

此代碼工作了歌廳start_addressend_addresshtml_instructions

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

工作Python腳本。請確保在您的環境中安裝googlemaps模塊,並在Google開發者控制檯和Google Directions API啓用時註冊了SERVER密鑰。

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