0

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.

blankpage985
  • 61
  • 11

2 Answers2

0

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.

geoffreyGIS
  • 353
  • 2
  • 5
  • 16
  • Apparently cartodb has its own behind the scenes geocoder. I assume I'm using that one. Come to think of it, I'm not sure that I want a pop-up asking to use their location, my native app already has their city and state. I'm thinking more along the lines of passing that in the search box on initialization. Cartodb comes with a search box and if you put in a zipcode it will take you there at the correct center and zoom. – blankpage985 Jun 26 '15 at 17:52
0

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);
Madness
  • 2,730
  • 3
  • 20
  • 29
skwidbreth
  • 7,888
  • 11
  • 58
  • 105