Reputation: 309
I'm having a problem accessing geolocation positions of markers and places.
Basically I am creating a marker for a user that has a position in the center of the map. Then I am running the geolocation.watchPosition
function, in which I am updating the userMarker
position. So with every tick done by watchPosition
, I am updating the location of the userMarker to their current location. I am also doing a very similar thing with some places
that I am searching within a given radius. The search is run with each tick of watchPosition
, and markers are given to each place
.
Visually, all markers are in the correct locations and update in the correct locations, so they are positioning right. The problem is that when I try to just print the positions of the markers to the console, using console.log(userMarker.getPosition())
, I am returned the marker's LatLng
object, but it empty and looks like this: _.E {}
.
I basically just want to know what am I doing wrong to access the marker positions and why? This can't just be how the objects are, it's definitely got to be something I am doing wrong.
Here is part of the code:
function updateMap(){
//navigator is passed a callback to handle return position
navigator.geolocation.watchPosition(watchPositionCallback);
}
/* HELPER FUNCTIONS AND CALLBACKS */
// callback for watchPosition() used to handle the returned position data
// as well as run searches
function watchPositionCallback(position){
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
}
//new google maps LatLng object
var currentLocation = new google.maps.LatLng(pos.lat, pos.lng);
//update request object to be sent to nearBySearch()
//with the currentLocation object as the specified location
nearByChurchSearchRequest.location = currentLocation;
nearByStoreSearchRequest.location = currentLocation;
// center the map on the currentLocation object
// and update userMarker position
map.setCenter(currentLocation);
userMarker.setPosition(currentLocation);
console.log(userMarker.getPosition()); // returns _.E {}
// initialize new PlacesServices
placesChurchService = new google.maps.places.PlacesService(map);
placesStoreService = new google.maps.places.PlacesService(map);
// service methods used to search nearby queries based on the requests
placesChurchService.nearbySearch(nearByChurchSearchRequest, searchCallback);
placesStoreService.nearbySearch(nearByStoreSearchRequest, searchCallback);
}
PS: I have also tried getCurrentLocation()
and that doesn't change anything either.
Upvotes: 0
Views: 789
Reputation: 1753
Call the methods on the LatLong object
your call to userMarker.getPosition() returns a LatLng object.
use .lat() and .lng() on this object as seen here
https://developers.google.com/maps/documentation/javascript/reference#LatLng
var pos = userMarker.getPosition();
console.log( pos.lat(), pos.lng() );
Upvotes: 1