在JavaScript中设置变量的值
正如您在下面的代码中所看到的,我正在从MySQL数据库获取数据,但是当我想使用获取的“位置”来设置google地图上标记的“位置”时,我遇到了麻烦在JavaScript中设置变量的值,javascript,jquery,Javascript,Jquery,正如您在下面的代码中所看到的,我正在从MySQL数据库获取数据,但是当我想使用获取的“位置”来设置google地图上标记的“位置”时,我遇到了麻烦 <head> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxxxxxx&sensor=true"></script> <script> $.get
<head>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxxxxxx&sensor=true"></script>
<script>
$.getJSON('http://www.wawhost.com/appProject/fetchmarker.php?callback=?', function(data) {
for (var i = 0; i < data.length; i++) {
localStorage.loc = data[i].location;
}
});
</script>
<script>
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var marker = new google.maps.Marker({
position: 'new google.maps.LatLng(' + localStorage.loc + ')',
map: map,
title: 'Hello World!'
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas"></div>
</body>
$.getJSON('http://www.wawhost.com/appProject/fetchmarker.php?callback=?,函数(数据){
对于(变量i=0;i
谢谢大家! 在页面加载后发出请求,并从AJAX成功回调调用
initialize
函数
$(function() {
$.getJSON('http://www.wawhost.com/appProject/fetchmarker.php?callback=?', function(data) {
for (var i = 0; i < data.length; i++) {
localStorage.loc = data[i].location;
}
initialize();
});
});
LatLng
采用纬度和经度参数来构造对象。从请求中存储的是一个字符串,用逗号分隔lat和long值
// First split the string into lat and long.
var pos = localStorage.loc.split(",");
// Parse the strings into floats.
var lat = parseFloat(pos[0]);
var lng = parseFloat(pos[1]);
// Create a new marker with the values from the request.
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map,
title: 'Hello World!'
});
加载JSON后,尝试调用
initialize()
。它应该是这样的->位置:new google.maps.LatLng(-25.363882131.044922)@alesko007-如果你知道它应该是什么样子,为什么不修复它呢。我做了些改变,让它在这里工作。请参阅编辑和fiddle链接。@alesko007-尝试使用控制台窗口记录和设置断点。你应该能够看到类型是错误的。我不熟悉javascript。谢谢你的帮助。我希望我能做好。祝你好运。确保在浏览器中使用开发人员工具(断点、日志记录、调用堆栈),这将为您省去很多麻烦。
// First split the string into lat and long.
var pos = localStorage.loc.split(",");
// Parse the strings into floats.
var lat = parseFloat(pos[0]);
var lng = parseFloat(pos[1]);
// Create a new marker with the values from the request.
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map,
title: 'Hello World!'
});