Pero sus ejemplos no son perfectos en uno de ellos se proponia establecer un autozoom a partir de los datos cargados desde xml, pero si en lugar de tener 10 puntos tenemos 2 0 1 y separados a corta distancia lo mas probable es que el zoom se establesca al maximo que da google maps, pero con el mensaje de error, sobre todo cuando se trata de zonas deshabitadas en donde los niveles de zoom son menores.
Por eso modifique un script hecho por el, me base en otro que encontre en la red, pero lamentablemente no recuerdo quien fue el autor:
El resultado final seria esto:
if (GBrowserIsCompatible()) {
var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var polys = [];
var labels = [];
var i = 0;
// === Specifiy Icons ====
var gicons = new GIcon();
GIcon.iconSize=new GSize(12,20);
GIcon.shadow = "img/mm_20_shadow.png";
GIcon.shadowSize=new GSize(22,20);
GIcon.iconAnchor=new GPoint(6,20);
GIcon.infoWindowAnchor=new GPoint(5,1);
GIcon.infowindowanchor=new GPoint(18,25);
GIcon.transparent = "img/mm_20_transparent.png";
// === Create an associative array of GIcons() ===
var gicons = [];
gicons["0"] = new GIcon(GIcon, "img/mm_20_3d_red.png");
gicons["1"] = new GIcon(GIcon, "img/mm_20_3d_blue.png");
gicons["2"] = new GIcon(GIcon, "img/mm_20_3d_yellow.png");
gicons["4"] = new GIcon(GIcon, "img/mm_20_3d_blueyellow.png");
// Function to specify the ZIndex of each marker according to the "importance" specified later.
// This is called in the function that creates the marker. The importance value is set during
// the loop through the points after the marker creation call is made, but before it is added to the map.
function setZIndex(marker,b) {
return marker.importance;
}
// the icon information is passed to this function
function createMarker(point,name,html,icontype,categoria) {
var marker = new GMarker(point, {icon: gicons[icontype], zIndexProcess:setZIndex});
marker.tooltip = ''+name+''; // store the tooltip text
marker.micategoria = categoria;
marker.elnombre=name;
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
// ==== save the info we need to use later for the side_bar
gmarkers[i] = marker;
htmls[i] = html;
var i2=i+1;
// ==== add a line to the side_bar html
if (name!="") side_bar_html += ''+i2+' '+name+'
';
i++;
// ====== The new marker "mouseover" and "mouseout" listeners ======
GEvent.addListener(marker,"mouseover", function() {
showTooltip(marker);
});
GEvent.addListener(marker,"mouseout", function() {
tooltip.style.visibility="hidden"
});
return marker;
}
// ====== This function displays the tooltip ======
// it can be called from an icon mousover or a side_bar mouseover
function showTooltip(marker) {
// only generate tooltip display if it contains real data (null value is )
if (marker.tooltip.length > 27) {
tooltip.innerHTML = marker.tooltip;
var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
var anchor=marker.getIcon().iconAnchor;
var width=marker.getIcon().iconSize.width;
var height=tooltip.clientHeight;
var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height));
pos.apply(tooltip);
tooltip.style.visibility="visible";
} // end if
} //end of functionshowTooltip
// ===== This function is invoked when the mouse goes over an entry in the side_bar =====
// It launches the tooltip on the icon
function mymouseover(i) {
showTooltip(gmarkers[i]);
}
// ===== This function is invoked when the mouse leaves an entry in the side_bar =====
// It hides the tooltip
function mymouseout() {
tooltip.style.visibility="hidden";
}
function myclick(i) {
gmarkers[i].openInfoWindowHtml(htmls[i]);
}
// set up for auto bounds calculation (bounds get extended as each point is read in)
var bounds = new GLatLngBounds();
var map = new GMap2(document.getElementById("map"));
// Put the loading message underneath the tiles
document.getElementById("map").style.backgroundImage = "url(515-500loading.jpg)";
var arribaDerecha = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,30));
var arribaDerecha2 = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(110,60));
map.addControl(new GLargeMapControl(), arribaDerecha2);
map.addControl(new GMapTypeControl(), arribaDerecha);
map.setCenter(new GLatLng(-33.1423258088452,-71.5579196742649),12);
map.setMapType(G_HYBRID_TYPE)
// ====== set up marker mouseover tooltip div ======
var tooltip = document.createElement("div");
map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
tooltip.style.visibility="hidden";
// ======== Add a scale in km, upper bar only
var _mPreferMetric = true;
var scale = new GScaleControl();
map.addControl(scale);
scale.fpsLbl.style.visibility="hidden";
scale.fpsBar.style.visibility="hidden";
// ========= cargar postes
var request = GXmlHttp.create();
request.open("GET", elservidor+"/xml/xml_pol_lineas.php?accion=postes&id_empresa="+id_empresa+"&periodo_star="+periodo_star+"&concesion_id="+concesion_id+"&poligonal_id="+poligonal_id+"&proyecto_id="+proyecto_id+"&linea_id="+linea_id, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
var xmlDoc = request.responseXML;
// ========= obtain the array of markers and loop through it
var postes = xmlDoc.documentElement.getElementsByTagName("poste");
var markerlengthss=postes.length;
if(markerlengthss>0){
for (var i = postes.length-1; i >=0; i--) {
// for (var i = 0; i < markers.length; i++) {
// =========obtain the attribues of each marker
var lat = parseFloat(postes[i].getAttribute("lat"));
var lng = parseFloat(postes[i].getAttribute("lng"));
var point = new GLatLng(lat,lng);
var html = postes[i].getAttribute("html");
var label = postes[i].getAttribute("label");
//mp var icontype = parseInt(markers[i].getAttribute("icontype"));
var icontype = postes[i].getAttribute("icontype");
// ======== create the marker
var poste = createMarker(point,label,html,icontype,'postes');
poste.importance = i;
if (icontype > 0) poste.importance = i+10000;
map.addOverlay(poste);
// ==== Each time a point is found, extend the bounds to include it =====
bounds.extend(point);
}
}
function autoReCenterZoom2 (bounds){
// Some map center and zoom must have been specified already - i.e. this does a re-center/zoom
// uses built-in bounds function
// Map center is average of max/min lat & long
var centerlat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
var centerlng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
// adjust if some points are across the dateline (i.e. >0 longitude) so the center point isn't in Africa
if (centerlng > -45) centerlng=centerlng+180;
map.setCenter(new GLatLng(centerlat,centerlng));
//built-in function to calc zoom
// set zoom out by 1 so points are not hidden by controls
var zoomb=map.getBoundsZoomLevel(bounds)-0;
var maptype = map.getCurrentMapType();
var nombredelmapa=maptype.getName(false);
switch(nombredelmapa)
{
case "Híbrido":
var laresolucion=G_HYBRID_MAP.getMaximumResolution();
break;
case "Satélite":
var laresolucion=G_SATELLITE_MAP.getMaximumResolution();
break;
case "Terreno":
var laresolucion=G_PHYSICAL_MAP.getMaximumResolution();
break;
case "Mapa":
var laresolucion=G_MAP_TYPE.getMaximumResolution();
break;
default : var laresolucion=G_MAP_TYPE.getMaximumResolution();
}
if(zoomb>laresolucion){
var resta=zoomb-laresolucion;
var newzoomb=zoomb-resta;
map.setZoom(newzoomb-1);
//alert(newzoomb);
}else{
map.setZoom(zoomb);
}
} //end of function autoCenterZoom
// ======== Add a map overview ==========
map.addControl(new GOverviewMapControl(new GSize(150,150)));
// ======== A function to adjust the positioning of the overview ========
function positionOverview(x,y) {
var omap=document.getElementById("map_overview");
omap.style.left = x+"px";
omap.style.top = y+"px";
// == restyling ==
omap.firstChild.style.border = "1px solid gray";
omap.firstChild.firstChild.style.left="2px";
omap.firstChild.firstChild.style.top="2px";
omap.firstChild.firstChild.style.width="145px";
omap.firstChild.firstChild.style.height="145px";
}
// ======== Cause the overview to be positioned AFTER IE sets its initial position ========
//setTimeout("positionOverview(558,254)",1);
// ========= Now process the polylines ===========
// var lines = xmlDoc.documentElement.getElementsByTagName("line");
// read each line
// for (var a = 0; a < lines.length; a++) {
// get any line attributes
// var colour = lines[a].getAttribute("colour");
// var width = parseFloat(lines[a].getAttribute("width"));
// read each point on that line
// var points = lines[a].getElementsByTagName("point");
// var pts = [];
// for (var i = 0; i < points.length; i++) {
// pts[i] = new GLatLng(parseFloat(points[i].getAttribute("lat")),
//// parseFloat(points[i].getAttribute("lng")));
// bounds.extend(pts[i]);
// }
// map.addOverlay(new GPolyline(pts,colour,width));
//}
// ================================================
// ========== Rellena la barra de contenidos con los datos de los postes
document.getElementById("side_bar").innerHTML = side_bar_html;
if(bounds.getSouthWest().lng()<180){
autoReCenterZoom2(bounds);
}
// map.setCenter(new GLatLng(-29.867, -71.241),12);
map.savePosition(); //store center/zoom for use by "return to last result" button
}
}
request.send(null);
}
// ==== fin de los postes
// == mostrar todos los puntos de una categoria determinada ==
function muestra(category) {
for (var i=0; iif (gmarkers[i].micategoria == category) {
gmarkers[i].show();
}
}
// == check the checkbox ==
document.getElementById(category+"box").checked = true;
}
// == hides all markers of a particular category, and ensures the checkbox is cleared ==
function esconde(category) {
for (var i=0; iif (gmarkers[i].micategoria == category) {
gmarkers[i].hide();
}
}
// == clear the checkbox ==
//document.getElementById(category+"box").checked = false;
// == close the info window, in case its open on a marker that we just hid
map.closeInfoWindow();
}
// == a checkbox has been clicked ==
function boxclick(box,category) {
if (box.checked) {
muestra(category);
} else {
esconde(category);
}
// == rebuild the side bar
makeSidebar();
}
function makeSidebar() {
var html = "";
for (var i=0; ivar i2=i+1;
if (!gmarkers[i].isHidden()) {
html += ''+i2+' '+gmarkers[i].elnombre+'
';
}
}
document.getElementById("side_bar").innerHTML = html;
}
/* // ==== save the info we need to use later for the side_bar
gmarkers[i] = marker;
htmls[i] = html;
var i2=i+1;
// ==== add a line to the side_bar html
if (name!="") side_bar_html += ''+i2+' '+name+'
';
i++;
// ====== The new marker "mouseover" and "mouseout" listeners ======*/
No hay comentarios:
Publicar un comentario