我有这段代码:
function initialize() {
var myLatlng = new google.maps.LatLng(0.0, 0.0);
var myOptions = {
zoom:7,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("cluster_map"), myOptions);
refreshMap();
}
function refreshMap() {
if (markerClusterer) {
markerClusterer.clearMarkers();
}
$.getJSON(__cfg('json_data_url'), function(data) {
if (data) {
for (var i = 0; i < data['Properties']['Count']; i ++ ) {
updateClusterMarker(data['Properties'][i]['Property'].latitude, data['Properties'][i]['Property'].longitude, data['Properties'][i]['Property'].id, i, 'Property');
}
for (var i = 0; i < data['Requests']['Count']; i ++ ) {
updateClusterMarker(data['Requests'][i]['Request'].latitude, data['Requests'][i]['Request'].longitude, data['Requests'][i]['Request'].id, i, 'Request');
}
var zoom = null;
var size = null;
var style = null;
markerClusterer = new MarkerClusterer(map, markers, {
maxZoom: zoom,
gridSize: size,
styles: styles[style]
});
}
});
}
function updateClusterMarker(lat, lang, id, count, type) {
var imageUrl = __cfg('path_relative') + 'img/R.png';
if (type == 'Property') {
var imageUrl = __cfg('path_relative') + 'img/P.png';
}
var markerImage = new google.maps.MarkerImage(imageUrl, new google.maps.Size(32, 32));
var latLng = new google.maps.LatLng(lat, lang);
eval('var marker' + count + ' = new google.maps.Marker({position: latLng,draggable: false,icon: markerImage});');
eval('marker' + count + '.count=1');
markers.push(eval('marker' + count));
var embed_url = __cfg('path_relative') + 'requests/get_info/' + id;
if (type == 'Property') {
var embed_url = __cfg('path_relative') + 'properties/get_info/' + id;
}
var contentString = '<iframe src="' + embed_url + '" width="279" height="120" frameborder = "0" scrolling="no">Loading...</iframe>';
eval('var infowindow' + count + ' = new google.maps.InfoWindow({ content: contentString, maxWidth: 300});');
var infowindow_obj = eval('infowindow' + count);
var marker_obj = eval('marker' + count);
google.maps.event.addListener(marker_obj, 'click', function() {
infowindow_obj.open(map, marker_obj);
});
}
它工作得很好,但是当我试图将地图中心与标记相同时,它将无法工作。我试过这段代码:
var myLatlng = new google.maps.LatLng(lat, lang);
有谁知道如何将地图置于与标记相同的位置? 任何帮助将不胜感激。
感谢
答案 0 :(得分:1)
您可以使用.getPosition()
方法获取标记位置(它返回LatLng
类对象)。因此,您可以使用类似map.panTo(marker.getPosition());
示例(jsFiddle):
window.onload = function(){
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(22.669, 77.709),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(22.669, 77.709),
map: map
});
$('button').on('click', function(){
map.panTo(marker.getPosition());
});
};
答案 1 :(得分:0)
我终于明白了:
styles.xml