2012-04-02 16 views
7

Apprezzerò molto l'aiuto per questo.Migrazione della mia API HTML Google MAP versione 2 alla versione 3

Il mio file html v2 con alcune chiavi temporanee funziona correttamente. Sto ottenendo luoghi da qualche XML, creo diversi marcatori di colori e aggiungo alcuni URL anche dagli attributi XML nella Finestra Informazioni (non troppo complicato). Ora ho bisogno di migrare questo alla v3. Ho trovato alcuni equivalenti per le funzioni dalla v2 ma non ho trovato per GDownloadUrl (per il caricamento di XML) e anche GIcon (G_DEFAULT_ICON); Qualcuno può per favore guardare entrambi i miei codici e dirmi come cambiare per farlo funzionare anche in v3. Ho cambiato la maggior parte delle cose quindi se qualcuno può vedere qualche errore, sarò grato. Grazie in anticipo.

Versione 2:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
    <head> 
    <title>Google Maps</title> 
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=AIzaSyA4UDNP6MZ" type="text/javascript"></script> 
    </head> 
    <body onunload="GUenter code herenload()"> 

    <!-- you can use tables or divs for the overall layout --> 
    <table border=1> 
     <tr> 
     <td> 
      <div id="map" style="width: 1250px; height: 1250px"></div> 
     </td> 
     </tr> 
    </table> 


    <script type="text/javascript"> 
    //<![CDATA[ 

    if (GBrowserIsCompatible()) { 

     var gmarkers = []; 

     // A function to create the marker and set up the event window 
     function createMarker(point,name,alarm,markerOptions) { 
     var marker = new GMarker(point,markerOptions); 
     GEvent.addListener(marker, "click", function() { 


     var alarmanchor1='<span class="url"><a href="' + alarm; 
     var alarmanchor2='" title="www" target="_blank">Event List</a></span>'; 
     var alarmanchor=alarmanchor1+alarmanchor2; 


     marker.openInfoWindowHtml(alarmanchor); 
     }); 

     return marker; 
     } 


     // This function picks up the click and opens the corresponding info window 
     function myclick(i) { 
     GEvent.trigger(gmarkers[i], "click"); 
     } 


     // create the map 
     var map = new GMap2(document.getElementById("map")); 
     map.addControl(new GLargeMapControl()); 
     map.addControl(new GMapTypeControl()); 
     map.setCenter(new GLatLng(41.932797,21.483765), 10); 


     // Read the data from alarms33.xml 
     GDownloadUrl("alarms33.xml", function(doc) { 
     var xmlDoc = GXml.parse(doc); 
     var markers = xmlDoc.documentElement.getElementsByTagName("marker"); 

     for (var i = 0; i < markers.length; i++) { 
      // obtain the attribues of each marker 
      var lat = parseFloat(markers[i].getAttribute("lat")); 
      var lng = parseFloat(markers[i].getAttribute("lng")); 
      var point = new GLatLng(lat,lng); 
      var alarm = markers[i].getAttribute("alarm"); 
      var label = markers[i].getAttribute("label"); 




     var severity = parseFloat(markers[i].getAttribute("severity")); 
     var severityIcon = new GIcon(G_DEFAULT_ICON); 
     var color; 
     if (severity == 0) color = "66FF33"; 
     else if (severity == 1) color = "990099"; 
     else if (severity == 2) color = "00CCFF"; 
     else if (severity == 3) color = "FFFF00"; 
     else if (severity == 4) color = "FFCC00"; 
     else if (severity == 5) color = "FF3300"; 
     else color = "yellow"; 

     severityIcon.image = "http://www.googlemapsmarkers.com/v1/" + color; 
     severityIcon.iconSize = new GSize(15, 30); 

     markerOptions = { icon:severityIcon }; 




      // create the marker 
      var marker = createMarker(point,label,alarm,markerOptions); 
      map.addOverlay(marker); 
     } 

     }); 
    } 

    else { 
     alert("Sorry, the Google Maps API is not compatible with this browser"); 
    } 



    //]]> 
    </script> 
    </body> 

</html> 

Versione 3:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
    <head> 
    <title>Google Maps</title> 
    <script src="http://maps.google.com/maps?file=api&amp;v=3&amp;sensor=false&amp;key=AIzaSyDsa1LyWOQ" type="text/javascript"></script> 
    </head> 
    <body onunload="initialize()"> 

    <!-- you can use tables or divs for the overall layout --> 
    <table border=1> 
     <tr> 
     <td> 
      <div id="map" style="width: 1250px; height: 1250px"></div> 
     </td> 
     </tr> 
    </table> 






    <script type="text/javascript"> 
    //<![CDATA[ 


     var gmarkers = []; 

     // A function to create the marker and set up the event window 
     function createMarker(point,name,alarm,markerOptions) { 
     var marker = new google.maps.Marker(point,markerOptions); 
     google.maps.event.addListener(marker, "click", function() { 


     var alarmanchor1='<span class="url"><a href="' + alarm; 
     var alarmanchor2='" title="www.skolaznanja.com" target="_blank">Event List</a></span>'; 
     var alarmanchor=alarmanchor1+alarmanchor2; 



     var infoWindow=new google.maps.InfoWindow(); 
     infoWindow.setContent(alarmanchor); 
     infowindow.open(map,marker); 

     }); 

     return marker; 
     } 


     // This function picks up the click and opens the corresponding info window 
     function myclick(i) { 
     google.maps.event.trigger(gmarkers[i], "click"); 
     } 


     // create the map 

     function initialize() { 
     var mapDiv = document.getElementById("map"); 
     var map; 
     var myLatlng = new google.maps.LatLng(41.932797,21.483765); 
     var myOptions = { 
     zoom:10, 
     center:myLatlng, 
     mapTypeId:google.maps.MapTypeId.ROADMAP 
     }; 
     map = new google.maps.Map(mapDiv, myOptions); 

    } 

     //var map = new google.maps.Map(document.getElementById("map")); 
     //map.addControl(new GLargeMapControl()); 
     //map.addControl(new GMapTypeControl()); 
     //map.setCenter(new google.maps.LatLng(41.932797,21.483765), 10); 


     // Read the data from example.xml 
     GDownloadUrl("alarms44.xml", function(doc) { 
     var xmlDoc = GXml.parse(doc); 
     var markers = xmlDoc.documentElement.getElementsByTagName("marker"); 

     for (var i = 0; i < markers.length; i++) { 
      // obtain the attribues of each marker 
      var lat = parseFloat(markers[i].getAttribute("lat")); 
      var lng = parseFloat(markers[i].getAttribute("lng")); 
      var point = new google.maps.LatLng(lat,lng); 
      var alarm = markers[i].getAttribute("alarm"); 
      var label = markers[i].getAttribute("label"); 




     var severity = parseFloat(markers[i].getAttribute("severity")); 
     var severityIcon = new GIcon(G_DEFAULT_ICON); 
     var color; 
     if (severity == 0) color = "66FF33"; 
     else if (severity == 1) color = "990099"; 
     else if (severity == 2) color = "00CCFF"; 
     else if (severity == 3) color = "FFFF00"; 
     else if (severity == 4) color = "FFCC00"; 
     else if (severity == 5) color = "FF3300"; 
     else color = "yellow"; 

     severityIcon.image = "http://www.googlemapsmarkers.com/v1/" + color; 
     severityIcon.iconSize = new GSize(15, 30); 

     markerOptions = { icon:severityIcon }; 




      // create the marker 
      var marker = createMarker(point,label,alarm,markerOptions); 
      map.setMap(marker); 
     } 

     }); 




    //]]> 
    </script> 
    </body> 

</html> 

risposta

5

Come hai notato GDownloadUrl() non esiste più in GMap V3. Suggerirei jQuery.get(url)

Ho pubblicato un esempio How to parse xml file for marker locations and plot on map.

UPDATE: Come @ user1191860 sottolinea seguito si trova un programma per GMap V3 xmlparsing. Non ne ero consapevole. AFAIK, nessun motivo per non usarlo. È necessario aggiungere

<script src="http://gmaps-samples-v3.googlecode.com/svn-history/r28/trunk/xmlparsing/util.js"></script> 

alla tua pagina html. Interessante che l'autore includa anche una jQuery example

+1

Ciao Eric. Ho trovato questo per API3 downloadUrl ("example.xml", function (doc) Perché non usare solo quello invece di jQuery? Questo è per API 3 sì? Penso che sia più semplice e soluzione più simile – Dejan

+0

Sì. Buona scoperta –

+0

Grazie a @Eric per lo sforzo, potresti aiutarmi con questo altro problema? In caso contrario, grazie comunque http://stackoverflow.com/questions/10013167/google-map-api-3-creating-different-colors-for -markers-from-code-of-api-2 – Dejan

Problemi correlati