我尝试使用从iframe获取的地图对象绘制圆圈,如下所示。
function modificaradio(){
var mapaZonas = geContentWindow(validateParentParentFrame('ifrmMapaZonasSeguras')).mapZone;
geContentWindow(validateParentParentFrame('ifrmMapaZonasSeguras')).geoDragzs.setMap(null);
var centroZonas = geContentWindow(validateParentParentFrame('ifrmMapaZonasSeguras')).newCenter;
geContentWindow(validateParentParentFrame('ifrmMapaZonasSeguras')).geoDragzs = drawCircle(centroZonas,radio, colorContorno, anchoContorno, opacidadContorno, colorRelleno, opacidadRelleno);
geContentWindow(validateParentParentFrame('ifrmMapaZonasSeguras')).geoDragzs.setMap(mapaZonas);
}
以下是绘制圆圈的功能
function drawCircle(center, radioCirculo,colorContorno,anchoContorno,opacidadContorno,colorRelleno,opacidadRelleno) {
nodes=40;
//se agrega un porcentaje al radio del circulo para su equivalencia en tamaño con mapInfo
var radioExtra = parseInt(radioCirculo * 0.10);
//el radio de la geocerca (dado en metros), se pasa a kilometros
var radioCirculoTot = (parseInt(radioCirculo) + radioExtra) / 1000;
var bounds = new google.maps.LatLngBounds();
var lat1 = new google.maps.LatLng(center.lat() + 0.1, center.lng());
var latConv = google.maps.geometry.spherical.computeDistanceBetween(center, lat1) / 100;
var lng2 = new google.maps.LatLng(center.lat(), center.lng() + 0.1);
var lngConv = google.maps.geometry.spherical.computeDistanceBetween(center, lng2) / 100;
//Loop
var points = [];
var step = parseInt(360 / nodes) || 10;
for (var i = 0; i <= 360; i += step) {
var pint = new google.maps.LatLng(center.lat() + (radioCirculoTot / latConv * Math.cos(i * Math.PI / 180)), center.lng() +
(radioCirculoTot / lngConv * Math.sin(i * Math.PI / 180)));
points.push(pint);
}
points.push(points[0]);
geocercaRadio = new google.maps.Polygon({
paths: points,
strokeColor: colorContorno,
strokeOpacity: opacidadContorno,
strokeWeight: 2,
fillColor: colorRelleno,
fillOpacity: opacidadRelleno
});
return geocercaRadio;
}
用于创建地图的功能如下所示。
function cargaMapaZonas(mapa,longitud, latitud, radio, idRadio){
zoomRadio = validaZoomRadio(idRadio);
var mapProp = {
center:new google.maps.LatLng(latitud,longitud),
zoom:zoomRadio,
mapTypeId:google.maps.MapTypeId.ROADMAP,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE
},
streetViewControl:false};
mapZone=new google.maps.Map(document.getElementById(mapa),mapProp);
radioGeo = radio;
newCenter = new google.maps.LatLng(latitud, longitud);
centroDragzs = agregaMarcadorGeo(imgCenterGeo, 10, 10, newCenter);
centroDragzs.setMap(mapZone);
// geoDragzs=drawCircle(radioGeo,newCenter,mapZone);
geoDragzs=drawCircle(newCenter,radioGeo, colorContorno, anchoContorno, opacidadContorno, colorRelleno, opacidadRelleno);
geoDragzs.setMap(mapZone);
//geoDragzs.bindTo('center', centroDragzs , 'position');
geoDragzs.clickable = false;
google.maps.event.addListener(centroDragzs, "click", function(){
var shape = {
coord: [1, 1, 1, 20, 18, 20, 18 , 1],
type: 'poly'
};
var bbMarker = new google.maps.Marker({
icon: 'img/visor/mm_20_red.png',
shadow: 'img/visor/mm_20_shadow.png',
shape: shape,
position: newCenter,
draggable:true,
map: map
});
bbMarker.setMap(mapZone);
geocoder = new google.maps.Geocoder();
google.maps.event.addListener(bbMarker, "dragend", function(){
geoDragzs.setMap(null);
newCenter = bbMarker.getPosition();
centroDragzs.setPosition(newCenter);
geoDragzs=drawCircle(newCenter,radioGeo, colorContorno, anchoContorno, opacidadContorno, colorRelleno, opacidadRelleno);
geoDragzs.setMap(mapZone);
bbMarker.setMap(null);
mapZone.panTo(newCenter);
parent.document.getElementById("longitud").value = newCenter.lng();
parent.document.getElementById("latitud").value = newCenter.lat();
showLocationfp(newCenter);
});
});
}
这就是我能够在调用cargaMapaZonas()时创建圆圈。但是当我从modificaradio()调用时,我得到"invalid value for property <map> <object,object>"
错误
有人可以指导我吗?
答案 0 :(得分:1)
当draw_circle
尚未定义时,draw_circle
不是null
,而是undefined
,并且比较将以错误结束。
检查类型:
if (typeof draw_circle !='undefined')
{draw_circle.setMap(null);}
相反,使用带有未定义对象的typeof
不会引发错误。
答案 1 :(得分:0)
一些想法:
draw_circle
,则可能不需要从函数返回draw_circle
。if(draw_circle)
即可。 center
已经是google.maps.LatLng
个对象,则无需构建另一个相同的对象。position
选项不存在(它不会造成任何伤害但也没有任何好处。)rad *= 1600 / 1000
看起来不对劲。 1600是(大约)从英里到米的转换,1000是从公里到米的转换。 1600/1000会有一些相当奇怪的转换(千分之一英里到米?)。提到iFrame尚不清楚。我不能对这方面发表评论。
function DrawCircleGeo(rad,center,map) {
rad *= 1600 / 1000;
if (draw_circle) {
draw_circle.setMap(null);
}
draw_circle = new google.maps.Circle({
center: center,
radius: rad,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: map
});
}