Does CartoDB have a function to change center location based on user location?

163 views Asked by At

I have a map that is integrated in my application that has data points nationwide. I would like to have the map open up at the users current location. I only see documentation for setting the center on init.

Is this even possible or should I look to Google Maps instead?

EDIT: I have the user location(city and state) in my native app's db. I would like to use that data and pass it to the map upon init.

2

There are 2 answers

0
skwidbreth On BEST ANSWER

Yep! Try this out...

navigator.geolocation.getCurrentPosition(loadPosition);

function loadPosition(position) {                
    lati = position.coords.latitude;
    longi = position.coords.longitude;
    map = L.map('map', {zoomControl: false}).setView([lati, longi], 15);

    L.tileLayer('BASE_LAYER_URL', {maxZoom: 19,}).addTo(map);

Ah just saw your edit... try setting the desired latitude and longitude as variables and then use the map.setView function (below), perhaps triggered on some user event...

    map.setView(new L.LatLng(latitudeVar, longitudeVar), 19);
1
geoffreyGIS On

Which geocoder are you using? I use leaflet.js and there is a share location pop-up that appears when I open my map and zooms to the users location. I am using an esri geocoder though.