Google Map marker is not in the center

I am embedding the Google Maps API and I am showing the map in the jQuery dialog. The map shows a penalty, but the marker position is not in the center, so please guide me.

How can I make the marker and map position in the center?

Code for card:

var myCenter = new google.maps.LatLng('@Model.Latitude', '@Model.Longitude'); 

function initialize() { 
    var mapProp = { 
        center: myCenter, 
        zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    var map = new google.maps.Map(document.getElementById("googleMap"), mapProp); 
    var marker = new google.maps.Marker({ 
        position: myCenter, 
        draggable: false, 
        animation: google.maps.Animation.DROP, 
    }); 

    marker.setMap(map); 
}

Code that opens the dialog box:

$("#googleMap").dialog({ 
    autoOpen: false, 
    height: 600, 
    width: 1000, 
    resizeStop: function (event, ui) { 
    google.maps.event.trigger(googleMap, 'resize') 
}, open: function (event, ui) { 
    google.maps.event.trigger(googleMap, 'resize'); }, 
}); 

$("#btnLocation").click(function () { 
    $("#googleMap").dialog("open"); 
});

HTML:

<div id="googleMap" style="width: 1000px; height: 500px"></div> 
<img src="map.jpg" alt="Set Location" id="btnLocation" style="cursor: pointer; height: 35px; width: 35px;" />
+3
source share
2 answers

Try the following:

1) Define mapand markeroutside the function initialize, we will call them later to center the map

var myCenter = new google.maps.LatLng('@Model.Latitude', '@Model.Longitude');

// DEFINE YOUR MAP AND MARKER 
var map = null, marker = null; 

function initialize() { 
    var mapProp = { 
        center: myCenter, 
        zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

     map = new google.maps.Map(document.getElementById("googleMap"), mapProp); 
     marker = new google.maps.Marker({ 
        position: myCenter, 
        draggable: false, 
        animation: google.maps.Animation.DROP, 
    }); 

    marker.setMap(map); 
}

2) Change the β€œopen” event of the dialog box to force the center of the map to open the dialog box:

$("#googleMap").dialog({ 
    autoOpen: false, 
    height: 600, 
    width: 1000, 
    resizeStop: function (event, ui) { 
        google.maps.event.trigger(googleMap, 'resize') 
    }, 
    // OPEN EVENT MODIFIED !
    open: function (event, ui) { 
        google.maps.event.trigger(googleMap, 'resize'); 

        // Force the center of the map
        map.setCenter(marker.getPosition());
    }, 
}); 

Try it and let me know if this works;)

+9
source

, .

, click.

map.setCenter('MARKER'.getPosition());

eventlistener :

google.maps.event.addListener(marker, 'click', function() {
   map.setCenter(marker.getPosition());
});

api- Google:

https://developers.google.com/maps/documentation/javascript/examples/event-simple

+2

All Articles