Last active
April 20, 2017 19:03
-
-
Save MaxPleaner/1263f7f6b8c40eeaa52b3f77a70dd2df to your computer and use it in GitHub Desktop.
meteor geolocation/maps excerpt (part of https://medium.com/p/adf3c3d1ebc6 tutorial)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Template.map.onCreated(function(){ | |
var self = this | |
GoogleMaps.ready('map', function(map) { | |
self.autorun(function(){ | |
self.map = map | |
plotAllUsers.bind(self)() | |
var user = Meteor.user() | |
if (user) { | |
setupReactiveVarsAndGetCoords(user).then(function(coords){ | |
[latitudeVal, longitudeVal] = coords | |
Meteor.call("updateUser", { | |
_id: user._id, latitude: latitudeVal, longitude: longitudeVal | |
}) | |
}).catch(function(err){ console.log("Geolocation not loaded") }) | |
} else { console.log("Not pinging server, not signed in") } | |
}) | |
}); | |
}) | |
// when called, is bound to a custom "this" object | |
function plotAllUsers() { | |
Meteor.users.find( | |
{}, { fields: { latitude: 1, longitude: 1, emails: 1 } } | |
).forEach(function(user){ | |
if (!(this.markers)) { this.markers = {} } | |
if (!(this.markers[user._id])) { | |
if (user.latitude && user.longitude) { | |
this.markers[user._id] = new google.maps.Marker({ | |
position: new google.maps.LatLng(user.latitude, user.longitude), | |
map: this.map.instance | |
}); | |
} | |
} else { | |
this.markers[user._id].setPosition({lat: user.latitude, lng: user.longitude}) | |
} | |
}.bind(this)) | |
} | |
function setupReactiveVarsAndGetCoords(user) { | |
var latLng = Geolocation.latLng(); | |
if (! latLng) { return Promise.reject(); } | |
if (!window.lat || !window.lng) { | |
window.lat = new ReactiveVar(latLng.lat), window.lng = new ReactiveVar(latLng.lng) | |
} | |
if (!(window.geolocationRandomnessInterval)) { | |
window.geolocationRandomnessInterval = window.setInterval(function() { | |
window.lat.set(Math.random() * 50) | |
window.lng.set(Math.random() * 50) | |
}, 1000) | |
} | |
console.log("coords changed") | |
return Promise.resolve([window.lat.get(), window.lng.get()]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment