javascript - Getting "Uncaught TypeError: Cannot read property 'lat' of undefined" with mapbox and leaflet -


i'm new mapbox , leaflet, , apologize in advance if bit basic. i'm trying load map markers. however, i'm getting error , i'm not sure how debug it. self.map.addlayer(connector) throwing error. know i'm doing wrong? thanks.

buildmap: function() {   console.log('buildmap() function...');   var lat, lng,   self = this;    navigator.geolocation.getcurrentposition(function(data) {     var lat, lng, latlng;     lat = data['coords']['latitude'];     lng = data['coords']['longitude'];     debugger;      self.map.remove();     self.map = new l.mapbox.map('map', 'bmy78.map-z27ovm6p')                             .setview(new l.latlng(lat, lng), 15);      console.log('lat', lat);     console.log('lng', lng);      // use mapbox     mapboxurl = "http://a.tiles.mapbox.com/v3/bmy78.map-z27ovm6p.jsonp";     wax.tilejson(mapboxurl, function(tilejson) {       connector = new wax.leaf.connector(tilejson);       self.map.addlayer(connector);     });   }); 

if want prevent map "undefined".

first declare map variable public js variable (in head of html document , outside function)

var map; 

next, build map inside document ready function (to sure leaflet load),

$( document ).ready(function() {   map= l.mapbox.map('map', 'bmy78.map-z27ovm6p')   //here try ask user position setview map } 

you may access map variable without "undefined error",

best regards


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -