javascript - Google Maps Address 而不是 LatLng GeoCode

标签 javascript google-maps-api-3 asp-classic latitude-longitude

我花了几个小时看这个,但找不到我想要的解决方案。我知道我遗漏了一些东西,但需要一点帮助。

本质上,我有一个带有保存地址的记录集的经典 ASP 页面。 我想将这些地址拉入页面(带有重复区域)。完成那一点,并获得 Google 期望的包含正确信息的字符串。

<script>var map_locations = [{"lat":52.954145,"lng":-4.101083,"title":"Nice House in Wales","street":"Prenteg, Porthmadog, LL49 9SR","price":"55,500"}]</script>

但是,我想用以下内容替换“lat”等:

<script>var map_locations = [{"address":"<%=(rsLocations.Fields.Item("locPostCode").Value)%>","title":"Nice house in Wales","street":"Prenteg, Porthmadog, LL49 9SR","price":"55,500"}]</script>

以下是 Google 根据以上链接建议我执行的操作。 Google 地理编码

var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
  zoom: 8,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
}

function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, 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
    });
  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});
}

<body onload="initialize()">
<div id="map_canvas" style="width: 320px; height: 480px;"></div>
<div>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Encode" onclick="codeAddress()">
</div>
</body>

现在,Google 的文档使用“地址”作为输入变量,因此您可以将其与以下内容一起使用:

var address = document.getElementById("address").value;

我的问题是,如何让记录集绑定(bind)成为地址? ASP 和 JS 文件不能很好地混合,并且不确定如何将其放入地址以使地理编码正常工作。

我的 JS 文件 (global.js) 包含以下内容,如果我通过 Lat/Lngs 发送,它会起作用,它会提供样式精美的标记和信息框供用户交互。

处理编写 Google map 内容的 JS 文件

var map;
function initializePropertiesMap() {
if($('#map_canvas').length == 0)
    return;
var myLatlng = new google.maps.LatLng(52.954145,-4.101083);
var myOptions = {
    zoom: 8,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
var infowindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
$.each(map_locations, function(key, value) { 
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(value['lat'], value['lng']),
        map: map,
        icon: 'css/images/marker.png',
        scrollwheel: false,
        streetViewControl:true,
        title: value['title']
    });

    var link = "link";
    google.maps.event.addListener( marker, 'click', function() {
        // Setting the content of the InfoWindow
        var content = '<div id="info" class="span5"><div class="row">' + '<div     class="span2"><img src="css/images/houses/house_'+(key+1)+'.jpg" class="thumbnail" style="width:135px"/></div>' + '<div class="span3"><h3>' + value['title'] + '</h3><h6>' + value['street'] + '</h6>' + '<strong>&pound;' + value['price'] + '</strong>' + '<p><a href="listing-detail.asp">Read More >></a></p>' + '</div></div></div>';
        infowindow.setContent(content );
        infowindow.open(map, marker);
    });

});


}

对此进行了调整后,我无法使其正常工作,想知道是否有人对我有任何建议?

谢谢 尼克

更新

只是想我会提到我已经尝试过这个:

var address = <%=rsLocations.Fields.Item("locPostcode").value%>;

这显然不会因为 JS 文件错误而起作用。

最佳答案

answer解决有关从 XML 对地址进行地理编码的类似问题可能会有所帮助。

You have a problem with the asynchronous nature of the geocoder, and if you add many addresses you will have a problem with the geocoder quota/rate limits (particularly since your code doesn't look at the return status of the geocoder).

The simplest solution is to use function closure to associate the call to the geocoder with the returned result

如果您有这些点的纬度和经度,请将其存储在您的数据库中并使用它来显示您的点。虽然您可以在页面加载时使用(异步)地理编码器对地址进行地理编码,但如果您有大约 10 个以上的地址,您将遇到地理编码器配额/速率限制。 This article on geocoding strategies可能有助于做到这一点。

关于javascript - Google Maps Address 而不是 LatLng GeoCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13350070/

相关文章:

javascript - 简单 <ul> <li> HTML 结构 : MegaMenu

javascript - 网络错误: XMLHttpRequest Exception 101 in Chrome

google-maps-api-3 - 在谷歌地图上渲染一个大圆圈而没有墨卡托投影失真?

javascript - 无法使用 javascript 在隐藏字段中显示值?

javascript - 如何在 Javascript 中循环函数?

php - Magento 联系表单重定向

javascript - 使用 $route 时 Google map 无法在 AngularJS 中工作

c# - 我应该开始学习 ASP Classic 还是 'continue' 学习 ASP.NET?

html - 不一致的#include css 外观

security - 我可以使用 cURL 绕过服务器的安全措施吗?