{"id":245,"date":"2012-06-27T22:55:29","date_gmt":"2012-06-28T05:55:29","guid":{"rendered":"http:\/\/shanelabs.com\/blog\/?p=245"},"modified":"2012-06-27T22:55:29","modified_gmt":"2012-06-28T05:55:29","slug":"even-better-geolocation-than-getposition-with-modified-watchposition","status":"publish","type":"post","link":"https:\/\/shanelabs.com\/blog\/2012\/06\/27\/even-better-geolocation-than-getposition-with-modified-watchposition\/","title":{"rendered":"Even better geolocation than getPosition() with modified watchPosition()"},"content":{"rendered":"<p>Part of the new geolocation api provides a method to get a user&#8217;s current position. In theory that&#8217;s awesome, but in practice it was returning inconsistent results. Sometimes it was accurate, other times it was off by miles. If I tried it multiple times in a row it would get more accurate, but only some of the time. The app I was working on needed better accuracy, so I tried a few things.<\/p>\n<p>First I tried specifying the precision parameter (by default it is set to low precision):<\/p>\n<blockquote><p>navigator.geolocation.getCurrentPosition(centerMap, centerMap, {enableHighAccuracy:true, maximumAge:0, timeout:30000});<\/p><\/blockquote>\n<p>This was MUCH more accurate, but it took about 45 seconds on my iphone to respond. Not okay. I looked to another solution: watchPosition(). It basically calls getCurrentPosition() multiple times so it can narrow in on where you are if you&#8217;re moving.<\/p>\n<blockquote><p>navigator.geolocation.watchPosition(centerMap);<\/p><\/blockquote>\n<p>Boom. Much better results. BUT it&#8217;s being called a lot, and I don&#8217;t want my map jumping around, I just want 1 accurate reading. I came up with the following solution:<\/p>\n<blockquote><p>var watchCount = 0;<\/p>\n<p>var watchId = navigator.geolocation.<wbr>watchPosition(centerMap);\u00a0<\/wbr><\/p>\n<p>function centerMap(location)<br \/>\n{<br \/>\nvar myLatlng = new google.maps.LatLng(location.<wbr>coords.latitude,location.<wbr>coords.longitude);<br \/>\nmap.setCenter(myLatlng);<br \/>\nmap.setZoom(17);<br \/>\nwatchCount++;<\/wbr><\/wbr><\/p>\n<p>\/\/do it 2 times<br \/>\nif(watchCount&gt;=2)<br \/>\n{<\/p>\n<p>\/\/show current location on map<br \/>\nvar marker = new google.maps.Marker({<br \/>\nposition: myLatlng,<br \/>\nmap: map,<br \/>\nicon: &#8216;<a href=\"http:\/\/www.google.com\/gmm\/images\/blue_dot_circle.png\" target=\"_blank\">http:\/\/www.google.com\/gmm\/<wbr>images\/blue_dot_circle.png<\/wbr><\/a>&#8216;,<br \/>\nclickable: false,<br \/>\nzIndex:1<br \/>\n});<br \/>\n\/\/don&#8217;t need to watch anymore<br \/>\nnavigator.geolocation.<wbr>clearWatch(watchId);<br \/>\n}<br \/>\n}<\/wbr><\/p><\/blockquote>\n<p>Basically it starts watchPosition until 2 positions are returned (I mentioned earlier how the 2nd result is always more accurate than the first), then it stops following the user. I set the threshold to 2 because the success callback is only called when there is a change in\u00a0location, so if I had set it higher and the user wasn&#8217;t moving I wouldn&#8217;t get what I want.<\/p>\n<p>Check out the <a href=\"http:\/\/dev.w3.org\/geo\/api\/spec-source.html#get-current-position\">spec<\/a> for more info!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Part of the new geolocation api provides a method to get a user&#8217;s current position. In theory that&#8217;s awesome, but in practice it was returning inconsistent results. Sometimes it was accurate, other times it was off by miles. If I tried it multiple times in a row it would get more accurate, but only some [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-software","category-web"],"_links":{"self":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":3,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":248,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/245\/revisions\/248"}],"wp:attachment":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}