罗伯托的帮助后编辑:
Python新手。该应用程序在前端使用Maps Javascript API v3。后端是webapp2 / GAE。我假设我也没有正确地做json。传递JSON后标记未正确创建。
q = Building.query()
buildings = q.fetch()
some_json = []
for building in buildings:
some_json.append(OrderedDict([('lat', float(building.lat)), ('lng', float(building.lng)), ('content', building.uuid)]))
appended_json = json.dumps(some_json)
HTML:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, #map-canvas { height: 100%; margin: 0; padding: 0;}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDMfzvCXM-LUe43cZ0mB2iDxx4aNOySkrQ">
</script>
<script type="text/javascript">
function initialize() {
var input_value = document.getElementById("lat").value;
var converted_value = JSON.stringify(input_value);
console.log(input_value);
var json = JSON.parse(converted_value);
console.log(json);
var locations = json;
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][2]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
<input id="lat" hidden="hidden" value="{{ appended_json }}">
</body>
</html>
如果输出正确并且没有引发任何错误,控制台将输出以下内容:
[{"lat": 39.2831792, "lng": -80.56741099999999, "content": "782f7100c13511e49f92ab5e18d4b38e"}] viewbuildings:17
[{"lat": 39.2831792, "lng": -80.56741099999999, "content": "782f7100c13511e49f92ab5e18d4b38e"}]
即使输出不正确,它仍然不会引发任何事情。
编辑:
确切的HTML浏览器看到如下
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, #map-canvas { height: 100%; margin: 0; padding: 0;}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDMfzvCXM-LUe43cZ0mB2iDxx4aNOySkrQ">
</script>
<script type="text/javascript">
function initialize() {
var input_value = document.getElementById("lat").value;
var converted_value = JSON.stringify(input_value);
console.log(input_value);
var json = JSON.parse(converted_value);
console.log(json);
var locations = json;
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][2]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
<input id="lat" hidden="hidden" value="[{"lat": 39.2831792, "lng": -80.56741099999999, "content": "782f7100c13511e49f92ab5e18d4b38e"}]">
</body>
</html>
答案 0 :(得分:0)
您的代码期望您的JSON看起来像这样:
[[39.2831792,-80.56741099999999,"782f7100c13511e49f92ab5e18d4b38e"]]
你的JSON看起来不像,看起来像:
[{"lat": 39.2831792,"lng": -80.56741099999999,"content": "782f7100c13511e49f92ab5e18d4b38e"}];
更改代码以处理您的JSON格式:
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i].lat, locations[i].lng),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i].content);
infowindow.open(map, marker);
}
})(marker, i));
}
您还错误地使用了JSON.stringify
和JSON.parse
。您的input元素的内容是一个JSON字符串,您只需将其传递给JSON.parse
以从中获取JSON对象:
工作代码段:
function initialize() {
var input_value = document.getElementById("lat").value;
var json = JSON.parse(input_value);
var locations = json;
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i].lat, locations[i].lng),
map: map
});
bounds.extend(marker.getPosition());
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i].content);
infowindow.open(map, marker);
}
})(marker, i));
}
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, 'load', initialize);
&#13;
html,
body,
#map-canvas {
height: 100%;
margin: 0;
padding: 0;
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry,places&ext=.js"></script>
<div id="map-canvas"></div>
<input id="lat" hidden="hidden" value="[{"lat": 39.2831792, "lng": -80.56741099999999, "content": "782f7100c13511e49f92ab5e18d4b38e"}]">
&#13;