In JavaScript, with <script async defer src="
https://maps.googleapis.com/maps/api/js?key=myKey&callback=myCallback";></script>
 
(in the HTML file), I've found that trying to get the user's position 
before the Map has been instantiated--so that said instantiatiion can be 
done with said position for center--using 
navigator.geolocation.getCurrentPosition through the callback, doesn't 
"work," because navigator.geolocation.getCurrentPosition must return 
immediately (according to its spec), but before the user's position can be 
obtained, s/he must first grant permission.  The upshot of this is that, 
seemingly, one must instantiate the Map with a LatLng constant (or, 
equivalently, literal), wait for permission to be sought and received, and 
then call navigator.geolocation.getCurrentPosition and re-center the Map 
using that result (which I've found to further require the user to perform 
some action, e.g., clicking on a button, to call Map.setCenter, but then, 
concerning this further requirement, maybe I'm missing some other way).  

However, if, for example, one launches Google Maps via the "nine squares" 
icon in the upper right corner of a Google page, the resulting map is 
automatically centered (roughly) on one's location upon initial 
presentation: there's no 
first-you-get-an-arbitrary-center-then-you-have-to-manually-recenter 
mumbo-jumbo (nor does one have to grant permission for this to work, 
interestingly).  How does Google Maps achieve all this?  Are they using 
some means other than navigator.geolocation.getCurrentPosition and/or 
<script async defer src="
https://maps.googleapis.com/maps/api/js?key=myKey&callback=myCallback";></script>?
 
 Are they simply "hiding" an arbitrarily-centered map until the user's 
location is obtained, then re-centering and "unhiding" the map (which I 
tried, but couldn't get to work, due to the apparent need for the user to 
interact in some way to call navigator.geolocation.getCurrentPosition)? 
 And how are they, seemingly, skipping the permission step?  

(What I've done/learned, so far, on my own to try to figure this out: 
first, as stated, I confirmed that Google Maps behaves in the way 
described/desired; then, I ctrl-clicked (I'm a Mac person) on the Google 
Maps App icon and selected Copy Link Address, obtaining the URL 
https://maps.google.com/maps?hl=en&tab=gl&authuser=0, which I then manually 
pasted into the address bar, and hit enter; I then observed that, *a few 
moments after the "correctly-centered" map is displayed*, the URL in the 
address bar automatically changes to 
https://www.google.com/maps/@<myLat>,<myLon>,13z?hl=en&authuser=0, 
i.e., one which includes my location's Lat and Lon: clearly, my location is 
being obtained and used to center the map *before* the map is displayed.  I 
also used the Chrome Developer Tools to look for and search what .js files 
I could find to try to locate navigator.geolocation.getCurrentPosition, to 
no avail, so either Google gets the user's location some other way, or I 
just couldn't find the .js file in which this function resides.)

Thanks for your help!

-- 
You received this message because you are subscribed to the Google Groups 
"Google Maps JavaScript API v3" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-maps-js-api-v3+unsubscr...@googlegroups.com.
To post to this group, send email to google-maps-js-api-v3@googlegroups.com.
Visit this group at https://groups.google.com/group/google-maps-js-api-v3.
For more options, visit https://groups.google.com/d/optout.

Reply via email to