views:

133

answers:

1

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..?

cheers douglas for the prompt reply.

I have found this example website from a previous question asked which sheds a bit of light. site is http://npdoty.name/location/

His example goes as follows:

navigator.geolocation.getCurrentPosition(function(position){
  var lat = position.coords.latitude;
  var lon = position.coords.longitude;
  var marker = new GMarker(new GLatLng(lat, lon));

  var jsMap = new GMap2(document.getElementById("jsMap"));
  jsMap.addOverlay(marker);
},function(error){
//use error.code to determine what went wrong
});

can i break out of this example and do something similar to:

var request = {
navigator.geolocation.getCurrentPosition(function(position){
  var lat = position.coords.latitude;
  var lon = position.coords.longitude;
  var myOrigin = new Gorigin(new GLatLng(lat, lon));

   origin: myOrigin, 
   destination: 'deleware',
   travelMode: google.maps.DirectionsTravelMode.DRIVING

};

many thanks

A: 

One of the things that may be causing a problem is the way you're trying to pass the myLat and myLong variables.

The value passed to origin needs to be a LatLng object.

i.e.

var myLat = "12.345";
var myLng = "54.321";
var myLatLng = new google.maps.LatLng(myLat, myLng);

var request = {
    origing: myLatLng,
    desitination...
    etc.
}

The biggest problem being that in order to pass the values, you need it to be formatted as such:

var request = {
    something: value,
    somethingElse: anotherValue,
    evenMore: bigValue
}

Note the commas after each line. This breaks in your example because of where you're placed yours in the origin value, thus confusing the property.

I'll keep looking at it, but hopefully this helps.

Douglas