jquery - Google map 标记不在中心

标签 jquery jquery-ui google-maps-api-3

我正在实现 Google Map API,并在 jQuery 对话框中显示 map 。 map 显示正常,但标记位置不在中心,所以请指导我。

如何使标记位置和 map 居中?

map 代码:

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); 
}

打开对话框的代码:

$("#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;" />

最佳答案

尝试以下操作:

1) 定义 mapmarker函数外 initialize ,我们稍后将调用它们来使 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)修改对话框的“打开”事件,打开对话框时强制显示 map 中心:

$("#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());
    }, 
}); 

尝试一下,让我知道这是否有效;)

关于jquery - Google map 标记不在中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21822705/

相关文章:

javascript - 单击模态外时暂停模态中的视频

javascript - 另一种预防垃圾邮件机器人的方法

javascript - Dygraph - 仅突出显示线下区域或多色背景取决于 Y 轴值

jQuery 可排序重新排序

javascript - 将相同的功能应用于所有以编程方式创建的 div

android - 更好地理解使用 Google Places Api

javascript - jquery $.ajax 调用在 Chrome 或 Firefox 中导致 401 未经授权的响应,但在 IE 中有效

javascript - 使用 highcharts.js 在每个 xAxis 类别标签上显示自定义图标图像 [html]

javascript - 如何在加载 google.maps.OverlayView 派生类之前等到 google maps API 加载完毕

html - 谷歌地图代码上的声音