2011-09-22 21 views
0

我有兩個位置(標記)要顯示在谷歌地圖上,一個是名爲「companyLocale」的靜態變量。第二個是基於您當前位置「initialLocation」的動態變量。我試圖將它們分組到一個名爲「localArray」的數組中,但我無法獲取「companyLocale」變量以在數組內顯示。有人可以幫幫我嗎?具有靜態變量的地理位置數組

<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Google Maps JavaScript API v3 Example: iPhone Geolocation</title> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 

var initialLocation; 
var companyLocale = new google.maps.LatLng(33.206060, -117.111951); 
var localArray = [initialLocation,companyLocale]; 

var noGeo = new google.maps.LatLng(40.69847032728747, -73.9514422416687); 

function initialize() { 
    var myOptions = { 
    zoom: 14, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

    // Safari supports the W3C Geolocation method 
    if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); 
     var placeMarker = new google.maps.Marker({ 
     position: initialLocation, 
     map: map, 
     }); 
     map.setCenter(initialLocation); 
    }, function() { 
     handleNoGeolocation(browserSupportFlag); 
    }); 
    } else { 
    // Browser doesn't support Geolocation 
    handleNoGeolocation(); 
    } 

    function handleNoGeolocation() { 
    initialLocation = noGeo; 
    map.setCenter(initialLocation); 
    } 
} 
</script> 
</head> 
<body style="margin:0px; padding:0px;" onload="initialize()"> 
    <div id="map_canvas" style="width:100%; height:100%"></div> 
</body> 
</html> 

回答

0

在調用initialize函數之前,您正在使用兩個值填充localArray。由於您已經將companyLocale定義爲LatLng,所以這樣做很好,但在執行後期纔會創建initialLocation LatLng。

我會說最初從數組中刪除initialLocation。然後,在創建它之後,將其添加到數組中。我不知道數組項目的順序在這裏對你有沒有重要。如果不是,只需使用.push()將其追加到數組中即可。否則,使用unshift()來加上它。

<script type="text/javascript"> 

var initialLocation; 
var companyLocale = new google.maps.LatLng(33.206060, -117.111951); 
var localArray = [companyLocale]; 

var noGeo = new google.maps.LatLng(40.69847032728747, -73.9514422416687); 

function initialize() { 
    var myOptions = { 
    zoom: 14, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

    // Safari supports the W3C Geolocation method 
    if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); 

     localArray.unshift(initialLocation); 

     var placeMarker = new google.maps.Marker({ 
     position: initialLocation, 
     map: map, 
     }); 
     map.setCenter(initialLocation); 
    }, function() { 
     handleNoGeolocation(browserSupportFlag); 
    }); 
    } else { 
    // Browser doesn't support Geolocation 
    handleNoGeolocation(); 
    } 

    function handleNoGeolocation() { 
    initialLocation = noGeo; 
    localArray.unshift(initialLocation); 
    map.setCenter(initialLocation); 
    } 
} 
</script> 
+0

鄧肯,謝謝你的迴應,我試了上面的代碼,但結果沒有改變。我仍然只獲取initialLocation變量來顯示,localArray仍然不會出現。請你再幫忙嗎? – need2nobasis

+0

那麼在代碼中,你沒有對localArray做任何事情,你是什麼意思「localArray仍然不會出現」 - 出現在哪裏/如何? – duncan

+0

我試圖在數組中獲取變量initialLocation和companyLocale,並使該數組中的每個變量都基於座標顯示在地圖上作爲標記。 – need2nobasis