javascript - Google Map not centring on marker -
the following code finding memberid on page , rendering map , placing marker not centring map on marker. map ending centred in presumably default location (somalia).
what doing wrong?
function initialize() { var memberid = $('#member-section').data("member-id"); if (typeof memberid === "undefined") { memberid = 0; } var latitude = $('#member-section').data("latitude"); if (typeof latitude === "undefined") { latitude = 51.21921589999999; } var longitude = $('#member-section').data("longitude"); if (typeof longitude === "undefined") { longitude = 4.402881799999932; } var mapcentrelatlng; mapcentrelatlng = new google.maps.latlng(latitude, longitude); buildmap(memberid, mapcentrelatlng); } function buildmap(memberid, mapcentrelatlng) { google.maps.visualrefresh = true; var infowindow = null; var mapoptions = { zoom: 5, center: mapcentrelatlng, maptypecontroloptions: { maptypeids: [google.maps.maptypeid.roadmap, 'map_style'] } }; var map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions); // create custom marker var markerimage = '/img/map-flag-alt.png'; // json data , loop create markers map $.getjson("/umbraco/api/searchmembers/getmembers?memberid=" + memberid, function (data) { $.each(data, function (members, member) { // add marker var marker; var memlatlng = new google.maps.latlng(member.latitude, member.longitude); marker = new google.maps.marker({ position: memlatlng, map: map, title: member.membername, icon: markerimage }); // add click event marker google.maps.event.addlistener(marker, 'click', function () { if (infowindow) { infowindow.close(); } // create info window , create content var contentstring = '<h2 id="firstheading">' + member.membername + '</h2>' + '<div id="bodycontent">' + '<p>' + member.address + '</p>' + '<p><a href="' + member.url + '">view company profile</a></p>' + '</div>'; infowindow = new google.maps.infowindow({ content: contentstring }); infowindow.open(map, marker); }); }); }); }
perhaps function initialize()
called before document finished loading vars definition did not find #member-section
. try either call function when dom loaded $(document).ready(function initialize(){...});
or add map.setcenter(memlatlng)
re-centre map per titus comment.
Comments
Post a Comment