Question

We're trying to build an app that allows users to view places around their current location.

You can see the Google Maps API & the Google Places Library functioning here, but not as one: http://www.blazingsasquatch.com/geo/index4.html

You'll notice the button "show me my loc" pulls your current location and the map is showing an arbitrary location in Boston with places nearby.

We've created variables for both the longitude and latitude and we've attempted to pass those variables directly into the "pyrmont" location variable but we've had no luck.

Initially we tried setting the "pyrmont" location using following, also with no luck: google.maps.LatLng(position.coords.latitude, position.coords.longitude);

So how can we get the current location populated?

var pyrmont = new google.maps.LatLng("CURRENT LOCATION HERE");

Will it accept a variable or an integer only?

Was it helpful?

Solution

initialize will be called before the callback of navigator.geolocation.getCurrentPosition() , so you cannot use the result in initialize() .(you may view getCurrentPosition as a asynchronous request)

Invoke the action(creation of the marker and places-request) inside the callback of getCurrentPosition()

See a working demo: http://jsfiddle.net/doktormolle/C5ZtK/

OTHER TIPS

The LatLng constructor takes two floating point numbers, like so:

pos = new google.maps.LatLng(-34.397, 150.644);

After reviewing the source code, it seems the problem is another one: the anonymous callback function you are passing to getCurrentPosition() is called when the geographic location is found, which can take some time (especially using GPS and such). initialize() is called when the page is loaded, which is sooner, so place is not set at this time (besides, it's not visible in initialize() since it's not a global variable, but never mind that). So you have to move the stuff you're currently doing in initialize() to the callback function you are passing to getCurrentPosition(). Then, you can use

var pyrmont = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top