User Location and API
by nygeog
HTML
<script src="https://libs.cartocdn.com/cartodb.js/v3/3.11/cartodb.js"></script>
<div id="map"></div>
CSS
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
JavaScript
var map;
function detectUserLocation(){
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.watchPosition(
mapToPosition,
alertError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
else {
alert("Geolocation is not supported by this browser");
}
function alertError(error) {
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[error.code]);
}
}
function main() {
cartodb.createVis('map', 'https://dms8md23.cartodb.com/api/v2/viz/e11893e4-88c8-11e5-8791-0ecfd53eb7d3/viz.json', {
title: false,
description: true,
search: true,
tiles_loader: true,
center_lat: 40.704424,
center_lon: -73.936596,
zoom: 17,
scrollwheel: false,
search: false,
shareable: false,
layer_selector: false,
mobile_layout: true,
})
.done(function(vis, layers) {
// layer 0 is the base layer, layer 1 is cartodb layer
// setInteraction is disabled by default
layers[1].setInteraction(true);
layers[1].on('featureOver', function(e, pos, latlng, data) {
cartodb.log.log(e, pos, latlng, data);
});
// you can get the native map to work with it
map = vis.getNativeMap();
// now, perform any operations you need
// map.setZoom(3);
// map.panTo([50.5, 30.5]);
detectUserLocation();
})
...