谷歌地图的纬度和经度是否有城市名称?

时间:2012-06-22 01:30:22

标签: javascript google-maps

我希望通过为API提供城市名称来获取城市的纬度和经度。无论用户如何输入城市,它都适用于大多数城市。

例如:

城市可以是'迈阿密,美国'或城市可以是'迈阿密,美国'

如何打印纬度?

3 个答案:

答案 0 :(得分:57)

您可以在此处找到jsfiddled代码:http://jsfiddle.net/YphZw/ 或以下:

$("#btn").click(function(){
            var geocoder =  new google.maps.Geocoder();
    geocoder.geocode( { 'address': 'miami, us'}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            alert("location : " + results[0].geometry.location.lat() + " " +results[0].geometry.location.lng()); 
          } else {
            alert("Something got wrong " + status);
          }
        });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
</head>
<body>
    <input id="btn" type="button" value="search for miami coordinates" />
</body>
</html>

如果您想了解更多Javascript API示例,请尝试以下链接:https://developers.google.com/maps/documentation/javascript/examples/

我写的代码的灵感来自 geocoding-simple 示例。

问候。

编辑1:

您可以使用非官方PHP库来实现它。检查此示例: http://www.bradwedell.com/phpgooglemapapi/demos/geocoding.php (代码位于底部=

答案 1 :(得分:13)

您可以使用Google的地理编码服务,例如

http://maps.googleapis.com/maps/api/geocode/xml?address=Miami+FL&sensor=false

它可以为您提供各种格式的地理配准数据(JSON,XML等)。无论如何,该位置肯定在返回的数据块中。

API文档位于:

https://developers.google.com/maps/documentation/geocoding/

答案 2 :(得分:2)

以下评论更新:在2018年7月之后无效。

这似乎是不必要的复杂。这是“附近事件”地图的示例。它需要City, State s,将它们转换为latLng坐标,并将标记放在地图上:

// Nearby Events with Google Maps
window.nearbyEventsMap = () => {
    const centerOfUS = {
        lat: 37.09024,
        lng: -95.712891
    }

    // Create a map object and specify the DOM element for display.
    const map = new google.maps.Map(document.querySelector('#nearby_events_map'), {
        center: centerOfUS,
        scrollwheel: false,
        zoom: 4
    })

    // Create a marker and set its position.
    const geocoder = new google.maps.Geocoder()

    // Filter out duplicate cityStates
    let cityStates = {}
    document.querySelectorAll('.nearby_event_city_state').forEach(event => {
        cityStates[event] = event.innerText
    })

    // `cityState` is in the format of "City, State". It's not picky about state being a word or the abbreviation.
    for (const cityState in cityStates) {
        const location = cityStates[cityState]

        geocoder.geocode({
            address: location
        }, function (results, status) {
            if (status === 'OK') {
                const result = results[0].geometry.location
                const lat = result.lat()
                const lng = result.lng()
                const latLng = {
                    lat,
                    lng
                }

                return new google.maps.Marker({
                    map: map,
                    position: latLng
                })
            }
        })
    }
}
// /Nearby Events with Google Maps

确保包含您的<script>代码。

<script src="/dist/js/main.js"></script>

<!-- We're loading this after the main.js script so the callback from main.js will be available to it. -->
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=nearbyEventsMap"></script>

StackOverflow请加入其他人并使用语法高亮显示获得GitHub降价...

相关问题