2011-09-22 4 views
0

Google지도에 표시 할 위치 (마커)가 두 개 있습니다. 하나는 "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

Duncan, 응답 해 주셔서 감사합니다. 위 코드를 시도했지만 결과는 변경되지 않았습니다. 여전히 initialLocation 변수 만 표시하면 localArray는 표시되지 않습니다. 이걸 다시 도울 수 있겠 니? – need2nobasis

+0

localArray에서 아무 것도하지 않는 코드에서 "localArray가 여전히 나타나지 않습니다"라는 의미는 무엇입니까? – duncan

+0

배열에서 변수 initialLocation과 companyLocale을 모두 가져 오려고 시도하고 해당 배열 내의 각 변수를 좌표에 따라 마커로 매핑에 표시하려고했습니다. – need2nobasis