basic javascript question on geolocation and googlemaps
- by ade
hello all,
Im hoping that somebody can help me out on what i feel is an easy answer but I just cant get it to work out.
Im trying to trap the geolocation lat and long and place it into the google maps api
so far i have
var myOptions = {
zoom:7,
trips:1,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
directionsDisplay.setMap(map);
var request = {
origin: 'newyork'
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
this works fine for what i want. how ever I want to be able to change Origin to the users lat long using
the following script from google.maps.api.
their code is:
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 = newyork;
map.setCenter(initialLocation);
}
I want to pull out the navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
and allocate it two 2 varibles, myLat and myLong.
I then want to be able to change my orignal script from
var request = {
origin: 'newyork'
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
to
var request = {
origin: myLat,myLong
destination: 'deleware',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
Does this make sense..?
Im currently having a pig with it and as im not a JS developer its what i think should be a simple bit of coding that im losing the battle with..
any thoughts..?