using get&watch position

124 views
Skip to first unread message

fireofhellx

unread,
Feb 19, 2011, 3:51:06 PM2/19/11
to google-map...@googlegroups.com
hi im trying to use getCurrentPosition and watchPosition at the same time my need for this is to set up the map centre on the user then for the user to move around the map with out it refreshing every time the user moves
this is what i have so far im just having problems adding in the getCurrentPosition
 
function success(position)
{
//get the latitude and longitude
var userlatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);


var myoptions =
  { 
center:userlatlng,
zoom:17,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
  }
  
  var map = new google.maps.Map(document.getElementById("mapdiv"), myoptions);


var image = 'images/um1.png';
var marker = new google.maps.Marker({position: userlatlng ,
map: map,
icon: image,
clickable: false
});
 }


function initialize()
{
 (navigator.geolocation)
{
navigator.geolocation.watchPosition(success);
}
}

Esa

unread,
Feb 19, 2011, 4:09:43 PM2/19/11
to Google Maps JavaScript API v3
You should initialize the map before calling geolocation because not
all browser support geolocation.

You can call map.setCenter() by getCurrentPosition and watchPosition.

fireofhellx

unread,
Feb 23, 2011, 2:20:32 PM2/23/11
to google-map...@googlegroups.com
thanks for that but still having issues with using both together. i have sort of got them both working but now i cant get a marker to go on the map (i think im just being dull) but here is wht i have done (ive most likely gone about this the wrong way but im new to all this and it sort of works)

var map;

 

function you(user)
{
var place = new google.maps.LatLng(user.coords.latitude, user.coords.longitude);

var image='images/um1.png';
//this is the marker that isnt coming up
var marker = new google.maps.Marker({position:place,
                                                                        map:map,
                                                                icon: image,
clickable: false
});
 
//marker.setMap(map);
  //alert("yo")
}


function success(poo)
{
//get the latitude and longitude
var setup = new google.maps.LatLng(poo.coords.latitude, poo.coords.longitude);
navigator.geolocation.watchPosition(you)
var myoptions =
  { 
center:setup,
zoom:17,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
  }
               
  var map = new google.maps.Map(document.getElementById("mapdiv"), myoptions);
  
 }


function initialize()

{
 (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(success);
}
}

as far as i can see the marker should come up i could be missing something small but any help would be amazing
oh and here is a link to it if that helps the marker should just move as the user dose.

Esa

unread,
Feb 23, 2011, 3:30:14 PM2/23/11
to Google Maps JavaScript API v3
navigator.geolocation.watchPosition() is triggered only when the
location changes. So you have to test it with a mobile device. Testing
with a desktop means just endless waiting:)

Just noticed that Firefox 3.6 returns more than just 'coords' now. It
returns also 'address' object with properties

streetNumber
street
city
county
country
countryCode
postalCode

All of them were correct in my case. Cool.
Reply all
Reply to author
Forward
0 new messages