我想从我的网络服务收到的数据中将标记添加到现有的Google地图中。我得到了纬度和经度没有问题。但我无法弄清楚如何做到这一点。这里是我的代码,以及我尝试做的事情:
<html>
<head>
...
<script>
function initialize(){
var mapProp = {
center:new google.maps.LatLng(25,-30),
zoom:2,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("googleMap") ,mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
...
<div class="col-md-8">
<div id="googleMap" style="width:700px;height:350px; border-radius:20px;"></div>
</div>
...
<script src="../front-end/js/main.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#jogadoresList").click(function () {
$("#index").hide();
$("#selecoes").hide();
$("#jogador").show();
initialize();
});
});
</body>
main.js
function findByIdLocationTwitterJogador(id){
console.log('findJTwitterByJogadorrrrrr AQUIIIIIIIIII' + id);
$.ajax({
type: 'GET',
url: rootURLLocalizacao +'/' + id,
dataType: "json", // data type of response
success: renderListLocalizacao
});
}
function renderListLocalizacao(data) {
// JAX-RS serializes an empty list as null, and a 'collection of one' as an object (not an 'array of one')
var list = data == null ? [] : (data.dados instanceof Array ? data.dados : [data.jogo]);
$.each(list, function(index, jogo) {
var myLatlng = new google.maps.LatLng(jogo.latitude,jogo.longitude);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Former About.com Headquarters"
});
});
}
答案 0 :(得分:0)
这里的问题是&#34;地图&#34; renderListLocalizacao(...)
函数无法看到对象。请注意您在map
initialize()
的方式
尝试将地图设为全局变量。如果您在运行该函数时转储/查看其值,我猜您会将map
视为undefined
。
答案 1 :(得分:0)
请按如下方式设置全局地图对象
<script>
var globalMap=null;
function initialize(){
var mapProp = {
center:new google.maps.LatLng(25,-30),
zoom:2,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
globalMap=new google.maps.Map(document.getElementById("googleMap") ,mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
并在renderListLocalizacao
function renderListLocalizacao(data) {
// JAX-RS serializes an empty list as null, and a 'collection of one' as an object (not an 'array of one')
var list = data == null ? [] : (data.dados instanceof Array ? data.dados : [data.jogo]);
$.each(list, function(index, jogo) {
var myLatlng = new google.maps.LatLng(jogo.latitude,jogo.longitude);
var marker = new google.maps.Marker({
position: myLatlng,
map: globalMap,
title:"Former About.com Headquarters"
});
});