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

iOS中基于Google Map SDK将城市矩形区域划分为均等网格并获取中心点经纬度

Split a City's Rectangular Bounding Box into Equal Sub-Regions & Get Center Coordinates (Google SDK)

Hey there! I get that you're working with the Google SDK, have a rectangular area mapped out for a city, and need to split it into evenly sized sub-regions while grabbing each sub-region's center latitude and longitude. Let's walk through a straightforward way to make this happen, since you didn't find luck on Stack Overflow earlier.

Core Logic First

Before diving into code, let's break down the basic math—this works regardless of which Google SDK flavor you're using:

  • Define your bounding box: You'll need the coordinates of the rectangle's southwest corner (min_lat, min_lng) and northeast corner (max_lat, max_lng).
  • Choose split dimensions: Decide how many rows (num_rows) and columns (num_cols) you want to split the rectangle into (e.g., 5x5 for 25 equal sub-regions).
  • Calculate step sizes:
    • Latitude step: (max_lat - min_lat) / num_rows (the vertical span of each sub-region)
    • Longitude step: (max_lng - min_lng) / num_cols (the horizontal span of each sub-region)
  • Compute center points: For each sub-region (i-th row, j-th column), the center is:
    • Center latitude: min_lat + (i + 0.5) * lat_step (midpoint between the top and bottom of the sub-region)
    • Center longitude: min_lng + (j + 0.5) * lng_step (midpoint between the left and right of the sub-region)

Example Code Snippets

Python (with Google Maps Python Client)

If you're using the Python SDK, here's a quick implementation to generate all center coordinates:

# Replace these with your city's actual bounding box coordinates
min_lat = 39.9042  # Southwest corner latitude
min_lng = 116.4074  # Southwest corner longitude
max_lat = 39.9774  # Northeast corner latitude
max_lng = 116.5174  # Northeast corner longitude

# Define how many rows/columns to split into
num_rows = 5
num_cols = 5

# Calculate step sizes
lat_step = (max_lat - min_lat) / num_rows
lng_step = (max_lng - min_lng) / num_cols

# Generate all center points
subregion_centers = []
for row in range(num_rows):
    for col in range(num_cols):
        center_lat = min_lat + (row + 0.5) * lat_step
        center_lng = min_lng + (col + 0.5) * lng_step
        subregion_centers.append((round(center_lat, 6), round(center_lng, 6)))

# Print or use the centers as needed
for idx, (lat, lng) in enumerate(subregion_centers, 1):
    print(f"Sub-region {idx}: Center at Lat {lat}, Lng {lng}")

JavaScript (Google Maps JavaScript API)

If you're building a web map with the JS API, this code will create markers for each sub-region center:

// Your map instance (replace with your existing map object)
const map = new google.maps.Map(document.getElementById("map"), {
  center: new google.maps.LatLng(39.9406, 116.4623), // City center example
  zoom: 12,
});

// Bounding box coordinates
const minLat = 39.9042;
const minLng = 116.4074;
const maxLat = 39.9774;
const maxLng = 116.5174;

// Split dimensions
const numRows = 5;
const numCols = 5;

// Calculate steps
const latStep = (maxLat - minLat) / numRows;
const lngStep = (maxLng - minLng) / numCols;

// Add markers for each center
for (let i = 0; i < numRows; i++) {
  for (let j = 0; j < numCols; j++) {
    const centerLat = minLat + (i + 0.5) * latStep;
    const centerLng = minLng + (j + 0.5) * lngStep;
    
    new google.maps.Marker({
      position: new google.maps.LatLng(centerLat, centerLng),
      map: map,
      title: `Center ${i * numCols + j + 1}`,
    });
  }
}

Key Notes

  • Adjust precision: Round the coordinates to 5-6 decimal places (standard for most mapping use cases) to avoid unnecessary precision.
  • Edge cases: If your rectangle crosses the 180° longitude line (unlikely for a single city), you'll need to handle the wrap-around logic—but this is rare for city-level bounding boxes.
  • SDK Integration: The core math stays the same; you just need to adapt it to the specific Google SDK you're using (Android, iOS, etc.) by using the platform's native latitude/longitude objects.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:29:14