Thinkerer
Thinkerer

Reputation: 1626

Somethings wrong with my Meteor Geolocation functions

Im unable to save the lat and lng of my coordinates. Im testing on a web browser so the coordinates should be available.

  1. Im testing on a web browser, have tried other websites and no issues getting lat and lng of my position
  2. The lat and lng appears on the html from {{loc.lat}} and {{loc.lng}}
  3. when I type 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.
  4. On submit of the message, error shows up on command prompt 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.
  5. Occasionally it returns this error: 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

Answers (2)

Thinkerer
Thinkerer

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

255kb - Mockoon
255kb - Mockoon

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

Related Questions