You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PySpark请求Google Maps API复杂JSON并提取所有lat与lng

Extracting All Lat/Long from Google Maps API JSON Response

Based on the JSON structure you shared, here's a practical way to pull out every lat (latitude) and lng (longitude) value across all relevant sections:

First, map out all paths with coordinates

From your structure, these are the key spots where lat/lng pairs live:

  • root.routes[].bounds.northeast (contains lat/lng)
  • root.routes[].bounds.southwest (contains lat/lng)
  • root.routes[].legs[].start_location (contains lat/lng)
  • root.routes[].legs[].end_location (contains lat/lng)

Example code (Python)

Python is a great tool for parsing JSON APIs, so here's a snippet that iterates through all possible paths and collects every coordinate pair—while handling nullable fields as noted in your structure:

import json

# Replace this with your actual API response string
api_response = """[Your Google Maps API JSON response here]"""
response_data = json.loads(api_response)

# Store all coordinates with context
all_coords = []

# Loop through each route in the routes array
for route in response_data.get('routes', []):
    # Extract bounds coordinates
    bounds = route.get('bounds')
    if bounds:
        # Northeast corner
        northeast = bounds.get('northeast')
        if northeast and 'lat' in northeast and 'lng' in northeast:
            all_coords.append({
                'source': 'bounds_northeast',
                'lat': northeast['lat'],
                'lng': northeast['lng']
            })
        # Southwest corner
        southwest = bounds.get('southwest')
        if southwest and 'lat' in southwest and 'lng' in southwest:
            all_coords.append({
                'source': 'bounds_southwest',
                'lat': southwest['lat'],
                'lng': southwest['lng']
            })
    
    # Extract coordinates from each leg
    legs = route.get('legs', [])
    for leg_idx, leg in enumerate(legs):
        # Start location of the leg
        start_loc = leg.get('start_location')
        if start_loc and 'lat' in start_loc and 'lng' in start_loc:
            all_coords.append({
                'source': f'leg_{leg_idx}_start',
                'lat': start_loc['lat'],
                'lng': start_loc['lng']
            })
        # End location of the leg
        end_loc = leg.get('end_location')
        if end_loc and 'lat' in end_loc and 'lng' in end_loc:
            all_coords.append({
                'source': f'leg_{leg_idx}_end',
                'lat': end_loc['lat'],
                'lng': end_loc['lng']
            })

# Print or process the collected coordinates
for coord in all_coords:
    print(f"{coord['source']}: lat={coord['lat']}, lng={coord['lng']}")

Key details to note:

  • We use .get() with default empty values to safely handle fields marked as nullable (so the code won't break if a section is missing)
  • The source field helps you track where each coordinate came from, which is useful for debugging or targeted processing
  • This code handles multiple routes and multiple legs per route, covering every possible lat/lng pair in your provided structure

内容的提问来源于stack exchange,提问作者rbcvl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:48:14