【问题标题】:Google Map Marker Is not in center谷歌地图标记不在中心
【发布时间】:2014-03-16 08:56:43
【问题描述】:

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

如何使标记位置和地图居中?

地图代码:

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;" />

【问题讨论】:

  • 嗨!你能显示一些代码吗?
  • 地图代码: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, });标记.setMap(地图); }
  • 这是地图代码。如果您愿意,我将向您发送 jquery 对话框代码。即使我无法发布图片,否则我会向您展示,您将完全解决问题。
  • 这可能很有用:)。你能添加打开对话框的代码吗?
  • $("#googleMap").dialog({ autoOpen: false, height: 600, width: 1000, resizeStop: function (event, ui) { google.maps.event.trigger(googleMap, 'resize') }, 打开: function (event, ui) { google.maps.event.trigger(googleMap, 'resize'); }, }); $("#btnLocation").click(function () { $("#googleMap").dialog("open"); });

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


【解决方案1】:

尝试以下方法:

1)在函数initialize之外定义mapmarker,我们稍后会调用它们来使地图居中

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)修改对话框的“open”事件,在打开对话框时强制地图居中:

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

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

【讨论】:

  • 我也做了同样的事情,但是标记的位置仍然在用户看不到的左上角。我们必须将地图向右移动才能查看标记。
  • 我尝试了同样的方法,但这不起作用。所以请您再检查一下并帮助我解决问题。
  • @Littm:您必须删除地图前面的 var 和函数 initialize() 内的标记。
  • @AntoJurković:谢谢安托。我按照你说的工作,效果很好。:)
  • @AntoJurković:对不起,我离线了,但我刚刚看到代码已更正^_^。感谢您的帮助。
【解决方案2】:

您必须将地图的中心设置为您刚刚单击的坐标位置。

通过将以下代码添加到您的点击事件处理程序来做到这一点。

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

这需要像这样添加到事件监听器中:

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

如需完整示例,请查看 google maps api 示例:

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

【讨论】:

  • 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, });标记.setMap(地图); }
  • 让我知道我必须在哪里添加更改。我做了但没有工作。我正在使用 jquery diloag 来显示地图。
  • 第一次加载地图时我没有得到中心位置。它在右上角。不向用户显示。
  • 我会等待启动谷歌地图,直到弹出窗口加载完毕。这将解决您的问题。
  • 你能告诉我我该怎么做吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 2012-09-01
  • 2021-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多