【问题标题】:How to geocode an address into lat/long with Google maps如何使用谷歌地图将地址地理编码为纬度/经度
【发布时间】:2011-11-21 22:15:04
【问题描述】:

我希望能够在谷歌地图上绘制几家公司,并了解我需要对这些公司进行地理编码。

我在地图上的多个标记下方也有代码。

如何对多个公司地址进行地理编码(使用以下地址作为第一个示例)并将其合并到我拥有的当前代码中?

我真的需要别人的帮助,因为我无法理解 Google 文档以及将其与我已有的文档相结合。

【问题讨论】:

    标签: google-maps google-maps-api-3 google-maps-markers


    【解决方案1】:

    您可以使用google's geocoding 获取您的邮政编码的坐标

    编辑:我真的不喜欢你这样改变问题的意义,但没关系。请尝试这样(未测试):

    // Creating an array that will contain the coordinates 
    // for New York, San Francisco, and Seattle
    var places = [];
    
    // Adding a LatLng object for each city
    //places.push(new google.maps.LatLng(40.756, -73.986));
    //places.push(new google.maps.LatLng(37.775, -122.419));
    //places.push(new google.maps.LatLng(47.620, -122.347));
    //places.push(new google.maps.LatLng(-22.933, -43.184));
    var result;
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "http://maps.googleapis.com/maps/api/geocode/json?address=your+code&sensor=false",true);
     xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
           result = eval('(' + xmlhttp.responseText + ')');
    if (result.status == "OK") {
    var location = new google.maps.LatLng(result.results[0].geometry.location.lat, result.results[0].geometry.location.lng);
    places.push(location);
    

    这应该可以工作,尽管可能有一些小错误。

    EDIT2:我刚刚找到simpler solution:

    // Creating an array that will contain the coordinates 
    // for New York, San Francisco, and Seattle
    var places = [];
    
    // Adding a LatLng object for each city
    //places.push(new google.maps.LatLng(40.756, -73.986));
    //places.push(new google.maps.LatLng(37.775, -122.419));
    //places.push(new google.maps.LatLng(47.620, -122.347));
    //places.push(new google.maps.LatLng(-22.933, -43.184));
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': "your+code"}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        //var marker = new google.maps.Marker({
            //map: map,
            //position: results[0].geometry.location
        //});
        places.push(results[0].geometry.location);
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
    

    【讨论】:

    • 谢谢,我不知道从哪里开始,更不用说将它与我拥有的当前代码合并了。
    • 查看我提供的网站链接和:w3schools.com/xml/xml_http.asp,只需构建您的请求,例如:maps.googleapis.com/maps/api/geocode/json?address=SW1A+0AA 来自谷歌的 eval json 响应,您就可以开始了
    • 哎呀忘了地理编码请求中的传感器:maps.googleapis.com/maps/api/geocode/json?address=SW1A+0AA&sensor=false
    • 感谢您的编辑。这适合我当前的代码吗?
    • 你应该把这些结果而不是带有 latLng 对象(地点)的硬编码数组,我将再次编辑答案以更清楚地显示它
    猜你喜欢
    • 1970-01-01
    • 2012-01-17
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2011-09-05
    相关资源
    最近更新 更多