Vi posto i blocchi di codice principali di una pagina nella quale ho
problemi ad aggiungere un marker:
Pagina.html
[...]
<body onload="init(45.95624,12.98004, false);">
[...]
<script>
var map_marker = new GMarker(
new GLatLng(
45.95624,12.98004 ),
{draggable:false}
); // Creo il marker per il dispositivo
GEvent.addListener(map_marker, "mouseover",
function() {
map_marker.openInfoWindowHtml("Prova");
}
); // Aggiungo balloon popup con le informazioni aggiuntive sul
dispositivo
</script>
[...]
<iframe src="iframe_map.html" style="overflow:hide; border:0px; margin:
0px; height: 400px; width: 580px;" id="map_iframe" name="map_iframe">
iframe_map.html
[...]
<body onload="init(null,null,false)" style="border:0px;margin:0px">
<div id="map" style="height: 400px; width: 580px;"> </div>
</body>
google_maps.js
/*
* Script per la gestione delle mappe di Google
*/
var map = null;
var geocoder = null;
// Se deve assegnare una posizione ad un record vuoto ...
var map_marker = null;
var map_markers = null;
var polylines = null;
var arrowsHead = null;
var arrowsMid = null;
var polygons = null;
var bounds = null;
var curves = null;
var contextmenu = null;
var overlays = null;
var loops = 0;
//20080416 10.35 - MP - Fix per mantenere alcuni overlays fissi.
var static_overlays = new Array();
//altrimenti se esiste già una coordinata da cui partire iniettare i
valori da php nel codice js come nell'esempio :
//var map_marker = new GMarker(new GLatLng(45.46591,9.183562),
{draggable:true});
function updatePos() {
//Aggiorna valore coordinate sui campi della form (a regime dovrebbe
restare solo quello nascosto)
// document.getElementById("position").value =
map_marker.getPoint().toUrlValue();
document.getElementById("visibleposition").value =
map_marker.getPoint().toUrlValue();
}
function mapClick(overlay, pt)
{
//Se non c'e' ancor un marker definito allora crealo e abilitalo,
altrimenti aggiorna solo la posizione
if (map_marker==null) {
map_marker = new GMarker(pt, {draggable:true});
GEvent.addListener(map_marker, "dragend", function()
{updatePos();} );
map.addOverlay(map_marker);
} else {
map_marker.setPoint(pt);
}
updatePos();
}
// === The basis of the arrow icon information ===
var arrowIcon = new GIcon();
arrowIcon.iconSize = new GSize(24,24);
arrowIcon.shadowSize = new GSize(1,1);
arrowIcon.iconAnchor = new GPoint(12,12);
arrowIcon.infoWindowAnchor = new GPoint(0,0);
// === Returns the bearing in degrees between two points. ===
// North = 0, East = 90, South = 180, West = 270.
var degreesPerRadian = 180.0 / Math.PI;
function bearing( from, to ) {
// See T. Vincenty, Survey Review, 23, No 176, p 88-93,1975.
// Convert to radians.
var lat1 = from.latRadians();
var lon1 = from.lngRadians();
var lat2 = to.latRadians();
var lon2 = to.lngRadians();
// Compute the angle.
var angle = - Math.atan2( Math.sin( lon1 - lon2 ) *
Math.cos( lat2 ), Math.cos( lat1 ) * Math.sin( lat2 ) -
Math.sin( lat1 ) * Math.cos( lat2 ) * Math.cos( lon1 - lon2 ) );
if ( angle < 0.0 )
angle += Math.PI * 2.0;
// And convert result to degrees.
angle = angle * degreesPerRadian;
angle = angle.toFixed(1);
return angle;
}
// === A function to create the arrow head at the end of the polyline
===
function headArrows(points) {
// == obtain the bearing between the last two points
var p1=points[points.length-1];
var p2=points[points.length-2];
var dir = bearing(p2,p1);
// == round it to a multiple of 3 and cast out 120s
var dir = Math.round(dir/3) * 3;
while (dir >= 120) {dir -= 120;}
// == use the corresponding triangle marker
arrowIcon.image = "http://www.google.com/intl/en_ALL/mapfiles/
dir_"+dir+".png";
//TODO: centrare l'icona con la punta della freccia (e non il
centro) sul marker
map.addOverlay(new GMarker(p1, arrowIcon));
}
// === A function to put arrow heads at intermediate points
function midArrows(points) {
for (var i=1; i < points.length-1; i++) {
var p1=points[i-1];
var p2=points[i+1];
var dir = bearing(p1,p2);
// == round it to a multiple of 3 and cast out 120s
var dir = Math.round(dir/3) * 3;
while (dir >= 120) {dir -= 120;}
// == use the corresponding triangle marker
arrowIcon.image = "http://www.google.com/intl/en_ALL/
mapfiles/dir_"+dir+".png";
//TODO: centrare l'icona con la punta della freccia (e non
il centro) sul marker
map.addOverlay(new GMarker(points[i], arrowIcon));
}
}
function resizeMap(points ) {
/*var minLong = 99999999;
var minLat = 99999999;
var maxLong = -99999999;
var maxLat = -99999999;
// Get the current map width/height
var size = map.getBounds();
var mapWidth = size.width;
var mapHeight = size.height;
var baseWidth = mapWidth;
var baseHeight = mapHeight;
// Figure out the elemental unit (depends on the size of the map)
// You will need to re-run resizeMap() if the size of the map
changes.
if ( map.getZoom() > 0 ) {
baseWidth /= Math.pow( 2, map.getZoom() );
baseHeight /= Math.pow( 2, map.getZoom() );
}
// Find the max/min points
for ( var i = 0; i < points.length; i++ ) {
if ( points[i].x < minLong ) minLong = points[i].x;
if ( points[i].x > maxLong ) maxLong = points[i].x;
if ( points[i].y < minLat ) minLat = points[i].y;
if ( points[i].y > maxLat ) maxLat = points[i].y;
}
// Find the optimal Width Zoom
var wZoom = 0;
var w = Math.abs( maxLong - minLong );
for ( var i = 1; i < 16; i++ ) {
if ( baseWidth > w ) break;
baseWidth *= 2;
wZoom = i;
}
// Find the optimal Height Zoom
var hZoom = 0;
var h = Math.abs( maxLat - minLat );
for ( var i = 1; i < 16; i++ ) {
if ( baseHeight > h ) break;
baseHeight *= 2;
hZoom = i;
}
//alert(wZoom+' '+hZoom);
// Reposition
map.setCenter(
new GLatLng( ( minLong + maxLong ) / 2, ( minLat + maxLat ) / 2 ),
( wZoom > hZoom ? wZoom : hZoom )
);*/
var _bounds = new GLatLngBounds();
if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();
for (i=0; i<points.length; i++) {
_bounds.extend(points[i]);
}
map.setZoom(map.getBoundsZoomLevel(_bounds));
map.setCenter(_bounds.getCenter());
}
}
function sleep(n)
{
var now = new Date();
var exitTime = now.getTime() + (n*1000);
while (true) {
now = new Date();
if (now.getTime() > exitTime) return;
}
}
function init(lat, lng, clk)
{
try {
// Inizializza oggetto mappa e assegnalo al div con id="map2
if (map == null) {
if (document.getElementById("map") == null) {
if (window.frames['map_iframe'].map == null) {
loops++;
sleep(1);
if (loops < 10) {
init(lat,lng,clk);
}
return;
} else {
map = window.frames['map_iframe'].map;
}
} else {
map = new GMap2(document.getElementById("map"));
}
}
//map = new GMap2()
geocoder = new GClientGeocoder();
// Centra mappa su Milano se non specificato dove centrare
if (lat==null) {
center = new GLatLng(45.466, 9.1873);
}
// Altrimenti centra mappa su punto passato
else
{
center = new GLatLng(lat, lng);
}
map.setCenter(center, 15);
// Aggiungi controlo zoom e pan
// map.addControl(new GSmallMapControl())
// Attiva gestione evento click su mappa
if (null == clk || true == clk)
{
GEvent.addListener(map, "click", mapClick);
}
// Se un marker è già settato abilita la visualizzazione
try {
if (map_marker!=null) {
//var punto_milano = new GLatLng(45.466, 9.1873);
//map.setCenter(markkkk, 15);
//map.setCenter(punto_milano, 15);
//map.addOverlay(new GMarker(punto_milano,{draggable:false}));
map.addOverlay(map_marker);
}
} catch (e1) {
alert(e1.description);
}
if (map_markers != null) {
for (i=0; i<map_markers.length; i++) {
map.addOverlay(map_markers[i]);
}
}
if (polylines!=null) {
for (i=0; i<polylines.length; i++) {
map.addOverlay(polylines[i]);
}
}
if (arrowsHead!=null) {
//for (i=0; i<arrows.length; i++) {
// map.addOverlay(arrows[i]);
//}
map.addOverlay(new GPolyline(arrowsHead));
headArrows(arrowsHead);
//midArrows(arrows);
}
if (arrowsMid!=null) {
//for (i=0; i<arrows.length; i++) {
// map.addOverlay(arrows[i]);
//}
map.addOverlay(new GPolyline(arrowsMid));
midArrows(arrowsMid);
}
if (polygons!=null) {
for (i=0; i<polygons.length; i++) {
map.addOverlay(polygons[i]);
//alert('aggiunto poligono '+i+' alla mappa!');
}
}
if (curves!=null) {
for (i=0; i<curves.length; i++) {
map.addOverlay(curves[i]);
//alert('aggiunto poligono '+i+' alla mappa!');
}
}
// Aggiungo controllo per zoom con scrollbar
map.addControl(new GLargeMapControl());
// Aggiungo controllo per tipo visualizzaizone map/hybrid/satellite
map.addControl(new GMapTypeControl());
map.addControl(new GScaleControl());
//20080121 11.58 MP Aggiunti listener per addoverlay, removeoverlay
e clearoverlays così tengo
// traccia di tutti gli overlays
inseriti. Pare che non esista
una cosa del
// tipo map.overlays o
map.getOverlays(). Si accettano
suggerimenti...
overlays = new Array();
GEvent.addListener(map,"addoverlay",function(overlay) {
overlays.push(overlay);
});
GEvent.addListener(map,"clearoverlays",function() {
overlays = new Array();
for (i=0; i<static_overlays.length; i++) {
map.addOverlay(static_overlays[i]);
}
});
GEvent.addListener(map,"maptypechanged",function() {
//TODO: ridisegnare le aree con colori diversi. (Giallo?)
});
if (bounds != null) {
resizeMap(bounds);
}
} catch (e) {
alert('Errore caricamento mappa! Error: '+e.description);
}
}
function enableRightClick() {
// === create the context menu div ===
contextmenu = document.createElement("div");
contextmenu.style.visibility="hidden";
contextmenu.style.background="#ffffff";
contextmenu.style.border="1px solid #8888FF";
contextmenu.innerHTML = '<a href="javascript:zoomIn()"><div
class="context"> Zoom in </div></a>'
+ '<a href="javascript:zoomOut()"><div
class="context"> Zoom out </div></a>'
+ '<a href="javascript:zoomInHere()"><div
class="context"> Zoom in here </div></a>'
+ '<a href="javascript:zoomOutHere()"><div
class="context"> Zoom out here </div></a>'
+ '<a
href="javascript:centreMapHere()"><div
class="context"> Centre map here </div></a>';
map.getContainer().appendChild(contextmenu);
// === listen for singlerightclick ===
GEvent.addListener(map,"singlerightclick",function(pixel,tile) {
// store the "pixel" info in case we need it later
// adjust the context menu location if near an egde
// create a GControlPosition
// apply it to the context menu, and make the context menu
visible
clickedPixel = pixel;
var x=pixel.x;
var y=pixel.y;
if (x > map.getSize().width - 120) { x = map.getSize().width -
120 }
if (y > map.getSize().height - 100) { y = map.getSize().height
- 100 }
var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new
GSize(x,y));
pos.apply(contextmenu);
contextmenu.style.visibility = "visible";
});
// === If the user clicks on the map, close the context menu ===
GEvent.addListener(map, "click", function() {
contextmenu.style.visibility="hidden";
});
}
// === functions that perform the context menu options ===
function zoomIn() {
// perform the requested operation
map.zoomIn();
// hide the context menu now that it has been used
contextmenu.style.visibility="hidden";
}
function zoomOut() {
// perform the requested operation
map.zoomOut();
// hide the context menu now that it has been used
contextmenu.style.visibility="hidden";
}
function zoomInHere() {
// perform the requested operation
var point = map.fromContainerPixelToLatLng(clickedPixel)
map.zoomIn(point,true);
// hide the context menu now that it has been used
contextmenu.style.visibility="hidden";
}
function zoomOutHere() {
// perform the requested operation
var point = map.fromContainerPixelToLatLng(clickedPixel)
map.setCenter(point,map.getZoom()-1); // There is no map.zoomOut()
equivalent
// hide the context menu now that it has been used
contextmenu.style.visibility="hidden";
}
function centreMapHere() {
// perform the requested operation
var point = map.fromContainerPixelToLatLng(clickedPixel)
map.setCenter(point);
// hide the context menu now that it has been used
contextmenu.style.visibility="hidden";
}
function showAddress(address, msg) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(msg);
} else {
bounds = new Array();
bounds.push(point);
if (circleEditorHandler != null) {
map.clearOverlays();
if (circleCenter != null) {
if (radius != null) {
circleCenter = null;
radius = null;
circleCenterMarker = null;
radiusMarker = null;
} else {
bounds.push(circleCenter);
}
}
onCircleMapClick(null, point);
} else if (boxEditorHandler != null) {
map.clearOverlays();
if (point1 != null) {
if (point2 != null) {
point2 = null;
point1 = null;
point2Marker = null;
point1Marker = null;
} else {
bounds.push(point1);
}
}
onBoxMapClick(null, point);
} else if (polygonEditorHandler != null){
onPolygonMapClick(null, point);
bounds = polyPoints;
} else {
var addressMarker = new GMarker(point);
map.addOverlay(addressMarker);
addressMarker.openInfoWindowHtml(address);
}
resizeMap(bounds);
//map.setCenter(point, 13);
}
}
);
return false;
}
Su firefox funziona perfettamente: quando imposto un map_marker,
l'init me lo mette nella mappa. Se inveceapro la stessa pagina su
internet explorer, mi dice "Argomento non valido". Qualcuno sa dirmi
perchè?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Maps API" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/Google-Maps-API?hl=en
-~----------~----~----~----~------~----~------~--~---