2011-09-22 40 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