Possible Duplicate:
JavaScript Variable Scope
i have problem with my script, i want to get value lat, lng from pos function, but when i inspect element in console i get this error :
"Uncaught ReferenceError: lat is not defined TEST.html:30 (anonymous function)"
line 30 is : var latlng = new google.maps.LatLng (lat, lng); This is my full code :
<script type="text/javascript">
navigator.geolocation.getCurrentPosition (function (pos){
lat = pos.coords.latitude;
lng = pos.coords.longitude;
});
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var latlng = new google.maps.LatLng (lat, lng);
var oceanBeach = new google.maps.LatLng(-6.2501199999999999,106.75937);
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: latlng
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var selectedMode = document.getElementById("mode").value;
var request = {
origin: latlng,
destination: oceanBeach,
// Note that Javascript allows us to access the constant
// using square brackets and a string value as its
// "property."
travelMode: google.maps.TravelMode[selectedMode]
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
</script>