Reputation: 1626
Im unable to save the lat and lng of my coordinates. Im testing on a web browser so the coordinates should be available.
{{loc.lat}}
and {{loc.lng}}
console.log(Geolocation.latLng());
, it returns the correct current lat and lng: Object {lat: xxx, lng: xxx};
and then a second line that shows undefined
but its not clickable.Exception while invoking method 'postInsert' MongoError: insertDocument :: caused by :: 16755 Can't extract geo keys from object, malformed geometry?
and field shows loc: { type: "Point", coordinates: [ null, null ] }
. I have checked that the order is correct as well.Uncaught TypeError: Cannot read property 'lat' of null
which I am unable to track since theres no mention of the problematic lines. It probably means loc
is null?Could someone help me on this? Thank you in advance :)
The below is my lines for the geolocation
In submit.js in client
Template.postSubmit.onCreated(function() {
this.interval = Meteor.setInterval(function (){
var location = Geolocation.latLng();
if(location) {
Session.set('location', location);
};
}, 2000);
Session.set('postSubmitErrors', {});
});
Template.postSubmit.helpers({
'loc': function () {
return Session.get('location');
}
});
Upvotes: 1
Views: 1101
Reputation: 1626
I found the solution. Problem lies with the structure of the loc in template.
coordinates: [loc.lat, loc.lng]
should be
coordinates: [loc.lng, loc.lat]
, as per Mongo's docs.
Upvotes: -2
Reputation: 6974
Geolocation.latLng()
triggers a refresh of the location but it is asynchronous, so it may return undefined at the beginning and then the coordinates a little later.
As there is no asynchronous callback in the mdg:geolocation package I am using a tracker to "wait" for the result of Geolocation.latLng()
:
var userGeoLocation = new ReactiveVar(null);
Tracker.autorun(function (computation) {
userGeoLocation.set(Geolocation.latLng());
if (userGeoLocation.get()) {
//stop the tracker if we got something
computation.stop();
}
});
Upvotes: 4