我正在尝试进行反向地理编码,我制作了一个快速代码片段来测试我的代码,看它是否有效。
这是以下代码:
<html>
<style>
/**
* Default attributes for gadget body.
*/
body {
font-family: Arial;
background: none transparent;
padding: 0px;
}
html, body, #map_canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<body>
<div id="map_canvas"></div>
<script>
var APIKey = "MyKeyValueIsInHere";
var geocoder;
var map;
var marker;
var locationTest = 'http://nominatim.openstreetmap.org/reverse?format=json&lat=55.653363&lon=12.547604&zoom=18&addressdetails=1';
var lat = 55.653363;
var lng = 12.547604;
function initialize() {
var latlng = new google.maps.LatLng(lat,lng);
// set the options for zoom level and map type
var myOptions = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
// create a map in the map_canvas div element
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
map.setCenter(latlng);
var marker = new google.maps.Marker({
map: map,
position: latlng
});
alert(locationTest);
var text = JSON.parse(locationTest);
var infoWindow = new google.maps.InfoWindow();
var houseNumber = text.address.house_number;
var road = text.address.road;
var suburb = text.address.suburb;
var zipCode = text.address.postcode;
var city = text.address.city;
var address = road + " " + houseNumber + ", " + zipCode + " " + suburb + " i " + city;
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(address);
infoWindow.open(map, this);
});
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?key=" + APIKey + "&sensor=false&callback=initialize";
document.body.appendChild(script);
}
loadScript();
</script>
</body>
</html>
无论如何,当我运行它时,我得到一个警告弹出说: http://nominatim.openstreetmap.org/reverse?format=json&lat=55.653363&lon=12.547604&zoom=18&addressdetails=1
基本上,我想要做的就是在我的谷歌地图上点击我的标记上的信息窗口。 infowindow应该有可变地址的文本。
但是当我运行此代码时,我得到了一个
Uncaught SyntaxError: Unexpected token h
第45行的是这一行:
var text = JSON.parse(locationTest);
那么有没有人知道我如何解决这个问题?
提前致谢!
答案 0 :(得分:0)
你有
var locationTest = 'http://nominatim.openstreetmap.org/reverse?format=json&lat=55.653363&lon=12.547604&zoom=18&addressdetails=1';
然后
var text = JSON.parse(locationTest);
但JSON.parse期望其参数是JSON编码的字符串:documentation。首先需要将网页的内容作为字符串,然后将其解析为JSON。错误消息是抱怨&#34; h&#34;在&#34; http&#34;的前面。它希望第一个角色是&#34; {&#34;。
在您的alert
声明之后,您可以尝试:
var $e = $('<div></div>'); // a jQuery object that does not affect your displayed page
$e.load('http://nominatim.openstreetmap.org/reverse?format=json&lat=55.653363&lon=12.547604&zoom=18&addressdetails=1', function (response) {
// This code only runs once the response is available from openstreetmap.org
var text = JSON.parse(response);
var infoWindow = new google.maps.InfoWindow();
var houseNumber = text.address.house_number;
var road = text.address.road;
var suburb = text.address.suburb;
var zipCode = text.address.postcode;
var city = text.address.city;
var address = road + " " + houseNumber + ", " + zipCode + " " + suburb + " i " + city;
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(address);
infoWindow.open(map, this);
});
});