Question

I have 3 maps from google map API on the same page. A main one, wider than the 2 others with a search field to find a city. There is a marker on it. I would like the user to "screen" the area while moving the marker with the result displayed on the 2 other maps.

E.g the user search for New York City, the main map will display the city with a zoom of 12. The 2 others the same point but with a zoom of 17 and 18. I would like that those 2 update there location once the user move the marker on the main map. For instance, if he drags the marker above Brooklyn bridge, the main window won't move but the 2 others will focus on the brooklyn bridge.

I have the following code which is working quite well to focus the 3 maps on the same point after a search in the textbox id=location, but I don't know where to add the function that would unable the update depending on the marker drag.

HTML

<div id="gmap" style="width:500px; height:500px;"></div>
<div id="gmap_sat" style="width:250px; height:150px;"></div>
<div id="gmap_zoom" style="width:250px; height:150px;"></div>

JS

<script type="text/javascript">
$(function(){
var latlng = new google.maps.LatLng(<?php echo $latlng?>);

var map = new google.maps.Map(document.getElementById('gmap'),{
    zoom: 12,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
});


var map_zoom = new google.maps.Map(document.getElementById('gmap_zoom'),{
    zoom: 15,
    center: latlng,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var map_sat = new google.maps.Map(document.getElementById('gmap_sat'),{
    zoom: 18,
    center: latlng,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.SATELLITE
});

var marker = new google.maps.Marker({
    position : latlng,
    map : map,
    title : 'Select a location',
    draggable : true
});

var geocoder = new google.maps.Geocoder();

google.maps.event.addListener(marker,'drag',function(){
    setPosition(marker);
});

$('#location').keypress(function(e){
    if(e.keyCode==13){
        var request = {
            address : $(this).val() 
        }
        geocoder.geocode(request,function(results, status){
            if(status == google.maps.GeocoderStatus.OK){
                var pos = results[0].geometry.location;
                map.setCenter(pos); 
                map_zoom.setCenter(pos);
                map_sat.setCenter(pos);
                marker.setPosition(pos);
                setPosition(marker);
            }
        });
        return false;
    }
})  
});

function setPosition(marker){
var pos = marker.getPosition(); 
$('#latitude').val(pos.lat());
$('#longitude').val(pos.lng());
}
</script>

Thanks for the help

Was it helpful?

Solution

Use Dragend event

<script>
    $(function(){
        var latlng = new google.maps.LatLng(43.723, -79.498);

        var map = new google.maps.Map(document.getElementById('gmap'),{
            zoom: 12,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });


        var map_zoom = new google.maps.Map(document.getElementById('gmap_zoom'),{
            zoom: 15,
            center: latlng,
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var map_sat = new google.maps.Map(document.getElementById('gmap_sat'),{
            zoom: 18,
            center: latlng,
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.SATELLITE
        });

        var marker = new google.maps.Marker({
            position : latlng,
            map : map,
            title : 'Select a location',
            draggable : true
        });

        var geocoder = new google.maps.Geocoder();

        google.maps.event.addListener(marker,'drag',function(){
            setPosition(marker);
        });

        google.maps.event.addListener(marker, 'dragend', function () {
            var newPosition = marker.getPosition();
            map_sat.setCenter(newPosition);
            map_zoom.setCenter(newPosition);
        });

        $('#location').keypress(function (e) {
            if(e.keyCode==13){
                var request = {
                    address : $(this).val() 
                }
                geocoder.geocode(request,function(results, status){
                    if(status == google.maps.GeocoderStatus.OK){
                        var pos = results[0].geometry.location;
                        map.setCenter(pos); 
                        map_zoom.setCenter(pos);
                        map_sat.setCenter(pos);
                        marker.setPosition(pos);
                        setPosition(marker);
                    }
                });
                return false;
            }
        })  
    });

    function setPosition(marker){
        var pos = marker.getPosition(); 
        $('#latitude').val(pos.lat());
        $('#longitude').val(pos.lng());
    }
      </script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top