打开 Bing Maps 世界地图并将图钉放在 Python 3 中的某个位置

Open a Bing Maps map of the world and put a pushpin on a location in Python 3

给定 Bing Maps API key、Python 3 和 Bing 城市地图上的位置,例如

import geocoder # pip install geocoder
g = geocoder.bing('Mountain View, CA', key=BING_KEY)
print(g.json['bbox'])

我如何在 Python 3 中使用 geocoder 有或没有另一个包,

Microsoft 显示 JavaScript 示例,但它似乎没有原生 Python API,而且我找不到 Bing 地图 API 对于 GitHub 上的图钉,只有少数 API 用于提取信息,但没有用于装饰地图的。

您可以在 Microsoft documentation 中看到您无法将图钉添加到 Bing 地图 URL。如果你真的想要图钉,你需要创建自己的网页,使用他们的 JavaScript API(见下文)嵌入 Bing 地图。

但是,您可以使用共享位置代替图钉,这会产生类似的效果。

如果您不介意,您可以执行以下操作:

import geocoder # pip install geocoder
import webbrowser
from urllib.parse import quote

BING_KEY = "..."
searchTerm = "Mountain View, CA"
g = geocoder.bing(searchTerm, key=BING_KEY)
lat = g.json['lat']
long = g.json['lng']
zoomLevel = 1 # Show entire world
url = f"https://www.bing.com/maps?cp={lat}~{long}&lvl={zoomLevel}&sp=point.{lat}_{long}_{quote(searchTerm)}"
webbrowser.open_new(url)

使用 JavaScript API

如果您确实想创建自己的页面而不是使用 Bing.com,最简单的方法可能是创建一个单独的 HTML 模板并从查询字符串中提取您需要的参数。此模板可以作为静态文件打开,从您计算机上的服务器本地提供,或托管在您自己的网站上。

文件 bing.html,与您的 Python 脚本位于同一目录中:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type='text/javascript'>
    function GetMap() {
        // Note Internet Explorer does not support URLSearchParams. It would need to parse manually.
        var urlParams = new URLSearchParams(window.location.search);

        var map = new Microsoft.Maps.Map('#myMap', {
            credentials: urlParams.get("key"),
            center: new Microsoft.Maps.Location(+urlParams.get("lat"), +urlParams.get("long")),
            zoom: +urlParams.get("zoom")
        });

        var center = map.getCenter();

        // Create custom Pushpin
        var pin = new Microsoft.Maps.Pushpin(center, {
            title: urlParams.get("title"),
            subTitle: urlParams.get("subTitle"),
            text: urlParams.get("label")
        });

        // Add the pushpin to the map
        map.entities.push(pin);
    }
    </script>
    <script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>
</head>
<body>
    <div id="myMap" style="position:relative;width:600px;height:400px;"></div>
</body>
</html>

那么您的 Python 脚本将与上面的其他选项略有不同:

import geocoder
import webbrowser
import pathlib
from urllib.parse import quote, urlencode

BING_KEY = "..."
searchTerm = "Mountain View, CA"
g = geocoder.bing(searchTerm, key=BING_KEY)
lat = g.json['lat']
long = g.json['lng']
zoomLevel = 1

queryString = urlencode({
    "key": BING_KEY,
    "lat": lat,
    "long": long,
    "title": searchTerm,
    "subTitle": "",
    "label": "1",
    "zoom": str(zoomLevel)
})
templateFile = "bing.html"
templatePath = pathlib.Path(__file__).parent.absolute().joinpath(templateFile)
url = f"file:///{templatePath}?{queryString}"

# If serving locally with `http.server` module:
# url = f"http://localhost:8000/{templateFile}?{queryString}"

browser = "chrome" # 'firefox' and 'safari' are also options here

# If opening as a static file, it MUST specify `browser`. The default will not work with a query string.
webbrowser.get(browser).open_new(url)