2012-04-04 39 views
0

我想從我的電腦上打開html文件http://www.geocodezip.com/v3_MW_example_map3.html。我可以在互聯網上打開它,但如果我查看頁面源代碼並將其另存爲HTML文件,我無法打開或者我可以但不顯示標記。 使用的XML位於http://econym.org.uk/gmap/example.xml,所以我也下載了它,並將它放在與html文件相同的文件夾中。如何解決這個問題?因爲我想改變這個文件,以適合我的需要 感謝谷歌地圖互聯網上的HTML例子不會在我的電腦上打開,但我可以在互聯網上打開網址

我的HTML(不包括如CSS沒有必要的東西,增加了..)現在看起來是這樣的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head> 

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 


<script type="text/javascript"> 
//<![CDATA[ 
     // this variable will collect the html which will eventually be placed in the side_bar 


     // arrays to hold copies of the markers and html used by the side_bar 
     // because the function closure trick doesnt work there 
     var gmarkers = []; 

    // global "map" variable 
     var map = null; 
// A function to create the marker and set up the event window function 
function createMarker(latlng, name, html) { 
    var contentString = html; 
    var marker = new google.maps.Marker({ 
     position: latlng, 
     map: map, 
     zIndex: Math.round(latlng.lat()*-100000)<<5 
     }); 

    google.maps.event.addListener(marker, 'click', function() { 
     infowindow.setContent(contentString); 
     infowindow.open(map,marker); 
     }); 
    // save the info we need to use later for the side_bar 
    gmarkers.push(marker); 

} 

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

function initialize() { 
    // create the map 
    var myOptions = { 
    zoom: 8, 
    center: new google.maps.LatLng(43.907787,-79.359741), 
    mapTypeControl: true, 
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, 
    navigationControl: true, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), 
           myOptions); 

    google.maps.event.addListener(map, 'click', function() { 
     infowindow.close(); 
     }); 
     // Read the data from example.xml 
     downloadUrl("example.xml", function(doc) { 
     var xmlDoc = xmlParse(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 html = markers[i].getAttribute("html"); 
      var label = markers[i].getAttribute("label"); 
      // create the marker 
      var marker = createMarker(point,label,html); 
     } 
     // put the assembled side_bar_html contents into the side_bar div 
     //document.getElementById("side_bar").innerHTML = side_bar_html; 
     }); 
    } 

var infowindow = new google.maps.InfoWindow(
    { 
    size: new google.maps.Size(150,50) 
    }); 


//]]> 
</script> 

    </head> 
<body style="margin:0px; padding:0px;" onload="initialize()"> 

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

     </tr> 
    </table> 

    <noscript><p><b>JavaScript must be enabled in order for you to use Google Maps.</b> 
     However, it seems JavaScript is either disabled or not supported by your browser. 
     To view Google Maps, enable JavaScript by changing your browser options, and then 
     try again.</p> 
    </noscript> 

//--></script> 
    </body> 
</html> 

回答

1

你不能做到這一點。 HTML將呈現,但由於所有不在本地計算機上的資源(css,js,圖像等),您將會遇到錯誤。您需要使用Maps API來構建您自己的解決方案。

+0

嗨喬感謝您的回答。我在HTML中刪除了所有不必要的東西。我的地圖已打開,但沒有標記。你看到我有什麼要刪除更多的工作來完成這項工作嗎?因爲我從計算機上下載了一些以前的示例 – Dejan 2012-04-04 12:55:09

+0

在您嘗試在本地重現的頁面源中,嘗試查找可能能夠複製到本地目錄的外部js和css資源。然後,將它們的引用連接到HTML中。這會讓你更接近。 – Joe 2012-04-04 13:03:08

+0

喬你解決了它。我只需要一個js文件而不是它的工作。如果你知道可能的解決方案,爲此我會很感激。 http://stackoverflow.com/questions/10013167/google-map-api-3-creating-different-colors-for-markers-from-code-of-api-2如果不是謝謝你! BR – Dejan 2012-04-04 14:34:28