I have been working with the google map api in a current project. I perform a query for nearby places using googles service.nearbySearch functionality. I then create a new google.maps.LatLng using the latitude and longitude values from the results of the nearbySearch. Below is the code for what I am doing:
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request,function(results,status){
for (var i = 0; i < results.length; i++) {
var place = results[i];
var pos = new google.maps.LatLng(place.geometry.location.A, place.geometry.location.k);
};
});
When I check the values of place.geometry.location.A and k when I run the program I have the correct values. However, when I look at the value of the newly created variable pos, the .k value has changed to -90 while the .A value remains correct.
Can anyone help me understand why this change occurs?
Furthermore, if I instead use the below snippet, the code works fine, but, I would like to understand why there's a difference.
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request,function(results,status){
for (var i = 0; i < results.length; i++) {
var place = results[i];
var pos = new google.maps.LatLng(place.geometry.location.lat(), place.geometry.location.lng());
};
});
Again checking the values of place.geometry.location.lat() and .lng() I can see that the values are correct and the same as the values from the previous code snippet. This time, however, the variable pos is created with the correct values and does not give an altered value of -90.
To make things clearer, below is the complete code that I am using. I am building this using the Ember.js framework if that's relevant.
The CSS
<script type="text/x-handlebars">
{{outlet}}
</script>
<script type="text/x-handlebars" id="map">
<div id="map_canvas" style="width:400px; height:400px"></div>
{{#view App.MapView}}{{/view}}
</script>
And the Javascript
App = Ember.Application.create();
App.Router.map(function(){
this.resource('map');
});
App.MapView = Ember.View.extend({
didInsertElement: function (){
var mapOptions = {
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//Creates map instance with given mapOptions and inserts it into html div
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
this.set('map', map);
navigator.geolocation.getCurrentPosition(function(position){
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(pos);
//Searches google places for nearby places (in this case: bars within 1500 meters of current location)
var request = {
location: pos,
radius: '1200',
types: ['bar']
};
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request,function(results,status){
var positions = [];
for (var i = 0; i < results.length; i++) {
var place = results[i];
var pos = new google.maps.LatLng(place.geometry.location.A, place.geometry.location.k);
// var pos = new google.maps.LatLng(place.geometry.location.lat(), place.geometry.location.lng());
if (place.geometry.location.lat() === place.geometry.location.k)
console.log("lats are equal - Place.geometry.location.lat() = " +
place.geometry.location.lat() + ' place.geometry.location.k = ' +
place.geometry.location.k + 'and the value for pos are - pos.A = ' + pos.A +
' pos.k = ' + pos.k);
else {
console.log("lats are not equal");
}
positions.pushObject(pos);
};
for(var i=0; i<positions.length;i++){
var marker = new google.maps.Marker({
position: positions[i],
map:map
});
};
});
})
}
})
Within this code I have included both of the ways that I define var pos. The top (uncommented line) does not work, but, the lower (commented out) way does. I also compare the values of both ways of defining the lat/lng values in the if-statement immediately following and can see that the values are interpreted as being completely equal.
Thanks in advance to anyone who can help me understand why my pos value incorrectly returns a lng value of -90 when using geometry.location.k, but, works correctly when using geometry.location.lng().
If further clarification, information, or anything is needed please let me know.