我需要数组中的图标,并且在比较获取图标数组之后,通过JSON获取数据,因此无法将它们作为对象告诉它们。
创建图标并将它们放入数组:
EMarkers = [];
var icon1e = {
nameIco: icon1e,
url: "./icon/sight.png",
size: new google.maps.Size(16, 16),
anchor: new google.maps.Point(8, 8),
};
EMarkers.push(icon1e);
var icon2e = {
nameIco: icon2e,
url: "./icon/museum21.png",
size: new google.maps.Size(16, 16),
anchor: new google.maps.Point(8, 8),
};
EMarkers.push(icon2e);
var icon3e = {
nameIco: icon3e,
url: "./icon/hram.png",
size: new google.maps.Size(16, 16),
anchor: new google.maps.Point(8, 8),
};
EMarkers.push(icon3e);
但是不会传输对象图标中的自定义选项以及标记。
jQuery.getJSON(url, function (json) {
jQuery.each(json, function(i, entry){
var _id = entry.event_id;
var _point = new google.maps.LatLng(entry.options.lat,entry.options.lng);
jQuery.each(EMarkers, function(idx, data){
if (data.nameIco == entry.options.icon){
entry.options.icon = data;
}
});
createMarker( _point, entry.options);
});
});
function createMarker(point, options){
var marker = new google.maps.Marker({
position: point,
map: map,
icon: options.icon,
title: options.title,
id: options.id
});
}
问题在于我无法仅将JSON图标用于标记作为字符串,这是我可以看到从数组中创建对象的唯一方法,但是如何从中获得所需的值?< / p>
答案 0 :(得分:0)
对象在之后可用,因此您在创建对象时无法使用对象。
这将产生预期的结果:
var icon1e = {
url: "./icon/sight.png",
size: new google.maps.Size(16, 16),
anchor: new google.maps.Point(8, 8),
};
icon1e.nameIco = icon1e;
...但我不建议,它会创建一个循环引用。
我不确定您要尝试实现的目标,但您的比较在任何情况下都可能失败,因为通过JSON通常无法传输类google.maps.Size
等类的实例。
你最好使用options.icon
的字符串(也许你已经这样做了)JSON项目(例如&#34; icon3e&#34;)并使用一个对象代替{{1}的数组您可以轻松地比较EMarkers
和entry.options.icon
EMarkers
.....
jQuery.getJSON(url,function(json){
EMarkers = {
icon1e = {
url: "./icon/sight.png",
size: new google.maps.Size(16, 16),
anchor: new google.maps.Point(8, 8),
},
icon2e = {
url: "./icon/museum21.png",
size: new google.maps.Size(16, 16),
anchor: new google.maps.Point(8, 8),
},
icon3e = {
url: "./icon/hram.png",
size: new google.maps.Size(16, 16),
anchor: new google.maps.Point(8, 8),
}
};
});
在这种情况下可以删除内部循环: jQuery.getJSON(url,function(json){
jQuery.each(json, function(i, entry){
var _id = entry.event_id;
var _point = new google.maps.LatLng(entry.options.lat,entry.options.lng);
jQuery.each(EMarkers, function(key){
if (key === entry.options.icon){
entry.options.icon = data;
}
});
createMarker( _point, entry.options);
});
});
答案 1 :(得分:0)
谢谢,你对格式字符串是正确的:
决定很简单:变量名必须是字符串,然后才能比较
相反:
var icon1e = {
nameIco: icon1e,
url: "./icon/sight.png",
size: new google.maps.Size(16, 16),
anchor: new google.maps.Point(8, 8),
};
使用:
var icon1e = {
nameIco: "icon1e",
url: "./icon/sight.png",
size: new google.maps.Size(16, 16),
anchor: new google.maps.Point(8, 8),
};
它有效:)