API Карт Google – Как рассчитать уровень масштабирования для заданных координатPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 API Карт Google – Как рассчитать уровень масштабирования для заданных координат

Сообщение Anonymous »

У меня есть следующий код:

Код: Выделить всё

def calculate_zoom_level(lat_diff, long_diff, map_width, map_height):
# Earth's circumference in degrees
WORLD_DIM = 256  # Tile size in pixels
ZOOM_MAX = 12    # Maximum zoom level (as used in the provided example)
MIN_DIFF = 0.000001  # Small value to prevent zero division

# A formula based on the map's bounds and dimensions to calculate zoom
def zoom_level(diff, map_dim):
# Avoid division by zero by using a minimum difference threshold
if diff == 0:
diff = MIN_DIFF
PADDING = 1.5  # Padding factor to leave space around the markers
return math.log2((360 / diff) * (map_dim / WORLD_DIM)) - PADDING

# Calculate zoom based on both latitude and longitude
lat_based_zoom = zoom_level(lat_diff, map_height)
long_based_zoom = zoom_level(long_diff, map_width)

# Take the smaller zoom level for fitting both dimensions into the map
zoom = min(lat_based_zoom, long_based_zoom)

# Further zoom out if necessary (based on location differences)
if lat_diff < 0.1 and long_diff <  0.1:
zoom -= 1  # Zoom out if locations are very close

# Constrain the zoom level to the maximum zoom specified (maxZoom = 12)
zoom = max(7, min(zoom, ZOOM_MAX))

return round(zoom)

def load_polygon_coordinates():
with open('cleaned_fine.json', 'r', encoding='utf-8') as file:
polygon_data = json.load(file)
return polygon_data

def generate_map_url(locations, polygons):
base_url = "https://maps.googleapis.com/maps/api/staticmap?"

# Extract latitudes and longitudes and convert them to floats
lats, longs = zip(*[(float(lat), float(long)) for lat, long in locations])

# Calculate bounds
min_lat, max_lat = min(lats), max(lats)
min_long, max_long = min(longs), max(longs)

# Set the center of the map to the midpoint of the bounds
center_lat = (min_lat + max_lat) / 2
center_long = (min_long + max_long) / 2

# Define the map dimensions (width and height in pixels)
map_width = 640
map_height = 530

# Calculate zoom level dynamically
zoom = calculate_zoom_level(max_lat - min_lat, max_long - min_long, map_width, map_height)

# Generate the markers for locations
markers = '|'.join([f'color:red|{lat},{long}' for lat, long in locations])

# Attempt to create polygon paths
paths = []
for polygon in polygons:
if len(polygon) >= 3:  # A polygon must have at least 3 points to form a closed shape
path = '|'.join([f'{lat},{long}' for lat, long in polygon])
# Ensure the polygon is closed by repeating the first point at the end
if polygon[0] != polygon[-1]:
path += f'|{polygon[0][0]},{polygon[0][1]}'
paths.append(f'color:0x00000000|weight:2|fillcolor:red|{path}')

# Set parameters for the static map API
params = {
'center': f'{center_lat},{center_long}',
'zoom': zoom,
'size': f'{map_width}x{map_height}',
'markers': markers,
'language': 'he',
'key': ''
}

# If no valid polygons are available, ensure only red markers are used
if paths:
params['path'] = paths

# Generate the map URL
url = base_url + urllib.parse.urlencode(params, doseq=True)

# Attempt to fetch the image to check if it generates correctly
try:
response = requests.get(url)
img = Image.open(BytesIO(response.content))  # This can raise the error you're seeing
except Exception as e:
print(f"Error generating map image: {e}")
# If there's an error, fallback to markers only
params.pop('path', None)  # Remove path if it exists
url = base_url + urllib.parse.urlencode(params, doseq=True)

return url
Проблема здесь в том, что иногда в любом случае слишком сильно уменьшаются области, которые расположены близко друг к другу.
Я ожидаю этого, согласно координаты, масштаб будет уменьшен в зависимости от получаемых областей, и что он не будет обрезан посередине или что-то в этом роде, он просто отобразит карту целиком, но только важную часть, а не просто большое уменьшение масштаба независимо от близости локаций друг к другу.
Прикрепляю для вас две картинки:
Изображение

Картинка с написанной на ней цифрой 1 является примером карты, которой я ожидаю.
Картинка с номером 2 это моя текущая карта (с этим кодом).
Я ожидаю, что она будет похожа на картинку номер 1, динамическое масштабирование в зависимости от местоположения

Подробнее здесь: https://stackoverflow.com/questions/791 ... oordinates
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»