Fixed Pointer Like Uber

323 views
Skip to first unread message

Michael Sacks

unread,
Nov 17, 2014, 6:49:38 PM11/17/14
to leafl...@googlegroups.com
I'd like to make an uber request ride like interface. By that I mean I want the pointer to remain in the center of the map at all times, I just want the user to be able to move the map to postition it so the pointer is on the correct location. 

ATM, I have the pointer default on their current location, but I want the user to be able to fine tune it to get the exact spot.


<div id="map">
            <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
            <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
            <script type="text/javascript">
              var map = L.map('map');

              L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map)

              map.locate({setView: true, maxZoom: 16});

              function onLocationFound(e) {
                var radius = e.accuracy / 2;

                L.marker(e.latlng).addTo(map).bindPopup("Your pickup spot is in this area").openPopup();
                L.circle(e.latlng, radius).addTo(map);
              }

              map.on('locationfound', onLocationFound);

              function onLocationError(e) {
                alert(e.message);
              }

              map.on('locationerror', onLocationError);

            </script>
          </div>


Bryan McBride

unread,
Nov 17, 2014, 10:31:42 PM11/17/14
to leafl...@googlegroups.com, michae...@gmail.com
You could use the map moveend event listener to set the marker location to the map center.

var marker = L.marker([0,0]).bindPopup("Your pickup spot is in this area").addTo(map);
function onMoveEnd(e) {
  marker.setLatLng(map.getCenter());
}
map.on('moveend', onMoveEnd);

Michael Sacks

unread,
Nov 20, 2014, 5:39:21 PM11/20/14
to leafl...@googlegroups.com, michae...@gmail.com
Awesome! Thanks Bryan!!
Reply all
Reply to author
Forward
0 new messages