Google Maps JS: click to enlarge Google Maps without moving the marker (e.g. Uber & Lyft)

I am currently building a Phonegap application along with the JS API.

I have a map that you can drag to select a location for the drop-down list. This is achieved by collecting the geolocation of the center of the map for the dragend event. Then I set a pin on top of the map (outside the JS map) so that you know where the center point is.

Everything works fine until you pinch it to zoom in and then popped. This is due to the fact that you scale and drag the map at the same time when you click on the screen.

Is there a way to prevent drag and drop while scaling to keep the map at its center point?

Please view JSfiddle here.

var map;
google.maps.event.addDomListener(window, 'load', onMapReady);


function onMapReady() {

    var center = new google.maps.LatLng(30.267153, -97.743061);
    var mapOptions = {
        center: center,
        zoom: 13,
        styles: [{"featureType": "poi", "stylers": [{ "visibility": "off" }]},{"featureType": "transit","stylers": [{ "visibility": "off" }]}],
        disableDefaultUI: true,
    };

    map = new google.maps.Map(document.getElementById('map'), mapOptions);

    google.maps.event.addListener(map, 'dragend', function() {
        center = map.getCenter();        
        $('#log').html('Lat: ' + center.lat() + ' Lng: ' + center.lng());
    });
}
+4
3

, , , , , , :

  • crosswalk , , , (iam intel xdk, , ).

  • UX: , , ! , .

  • , , , ​​ this, this , , , , , , , false, true.

, , .

+2

EDIT: ProllyGeek , zoom_changed , . , ( ) :

google.maps.event.addListener(map, 'dragend', function() {
    if (center && center != map.getCenter()) {
        map.setCenter(center);
        center = null;
    }
});

//we should recenter the map if the click/mousedown was within the centerRadius of the center of the map
google.maps.event.addListener(map, 'mousedown', function(clickMouseEvent) {
    var mapDiv = document.getElementById('map');

    if (pointInCircle(clickMouseEvent.pixel.x, 
                      clickMouseEvent.pixel.y, 
                      mapDiv.offsetWidth/2, 
                      mapDiv.offsetHeight/2, 
                      centerRadius)) { 
        //map.setOptions({draggable: false}); //disables zoom and dragging
        center = map.getCenter(); //save the current center point so we can recenter later.
    }
});

//handy function to see if a x,y coordinate is within z radius of another x,y coordinate
//from https://stackoverflow.com/a/16819053/1861459
function pointInCircle(x, y, cx, cy, radius) {
    var distancesquared = (x - cx) * (x - cx) + (y - cy) * (y - cy);
    return distancesquared <= radius * radius;
}

http://jsfiddle.net/bxfn499f/11/

:

draggable false ?

google.maps.event.addListener(map, 'zoom_changed', function() {
    map.setOptions({draggable: false});
    //heavy handed re-enabling of draggable
    //setTimeout(function() { map.setOptions({draggable: true}); }, 5000); //cant drag for five seconds
});

mouseup ( touchend), - (map.setOptions({draggable: true});). :

google.maps.event.addListener(map, 'mouseup', function() {
    map.setOptions({draggable: true});
});

http://jsfiddle.net/bxfn499f/6/ , , - - , window.load $(document).ready(function() { ... }. , , .

+1

, . , , .

div . . "center map" div div. dragend .

, , .

+1

All Articles