Search code examples
javascriptmeteorgeolocation

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');
      }
    });

Solution

  • 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.