Way*_*man 2 jquery google-maps
我已经将不久前为另一个 Web 应用程序编写的代码用于另一个 Web 应用程序。但是,由于没有明显的原因,它没有检索结果:
\n\n\n\n\n由于以下原因,地理编码未成功:ZERO_RESULTS
\n
我有以下代码,其中 \xe2\x80\x94 暗示 \xe2\x80\x94 在其他地方正常运行:
\n\n<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>\n<script type="text/javascript">\n function googleMaps (lat, lng, zoom) {\n geocoder = new google.maps.Geocoder();\n var myLatlng = new google.maps.LatLng(lat, lng);\n var myOptions = {\n zoom: zoom,\n center: myLatlng,\n mapTypeId: google.maps.MapTypeId.TERRAIN\n }\n map = new google.maps.Map(document.getElementById("map-locations"), myOptions);\n var marker = new google.maps.Marker({\n position: myLatlng,\n map: map\n });\n google.maps.event.addListener(map, "center_changed", function(){\n document.getElementById("latitude").value = map.getCenter().lat();\n document.getElementById("longitude").value = map.getCenter().lng();\n marker.setPosition(map.getCenter());\n document.getElementById("zoom").value = map.getZoom();\n });\n google.maps.event.addListener(map, "zoom_changed", function(){\n document.getElementById("zoom").value = map.getZoom();\n });\n }\n $(document).ready(function() {\n googleMaps(52.3555, -1.1743, 6);\n });\n</script>\n<script>\n $(document).ready(function(){\n $("#address").change(function(){\n geocoder.geocode({\n "address": $(this).attr("value")\n }, function(results, status) {\n if (status == google.maps.GeocoderStatus.OK) {\n map.setZoom(14);\n map.setCenter(results[0].geometry.location);\n } else {\n alert("Geocode was not successful for the following reason: " + status);\n }\n });\n });\n })\n</script>\nRun Code Online (Sandbox Code Playgroud)\n\n我还有“画布”和相应的地址字段,它们与其他地方的功能版本相同。
\n\n顺便说一句,我通过 Google API 过程创建了一个 API 密钥,这似乎与早期版本无关。但无论有没有 API 密钥,它都无法运行。
\n\n所以我想知道这个过程是否以某种方式破坏了事情。
\n你必须将你的代码更改为
\n\n...\ngeocoder.geocode({\n "address": $("#address").val()\n }, function(results, status) {\n...\nRun Code Online (Sandbox Code Playgroud)\n\n使用$("#address").val()而不是$("#address").attr("value").
更新:另请参阅SO问题jQuery $obj.val() vs $obj.attr(\xe2\x80\x9cvalue\xe2\x80\x9d)
\n