我正在使用Google Map V3来显示从数据库中获取的标记列表,使用MySQL和PHP。 我的标记是使用完整地址(来自数据库,包括邮政编码)设置在地图上的,因为我没有经纬度信息。 一切正常,我的循环如下所示:
while...
Addmarker(map,'MY ADdress','Title');
end while;
现在我想将地图设置为特定的标记,该标记包含在与先前输入的邮政编码相匹配的循环中。如何将地图设置为以该标记为中心并打开信息窗口?
我正在使用Google Map V3来显示从数据库中获取的标记列表,使用MySQL和PHP。 我的标记是使用完整地址(来自数据库,包括邮政编码)设置在地图上的,因为我没有经纬度信息。 一切正常,我的循环如下所示:
while...
Addmarker(map,'MY ADdress','Title');
end while;
现在我想将地图设置为特定的标记,该标记包含在与先前输入的邮政编码相匹配的循环中。如何将地图设置为以该标记为中心并打开信息窗口?
一旦您在地图上放置了标记,您可以通过API检索纬度/经度坐标,并使用它来设置地图的中心。 您首先需要确定要将哪个标记设置为中心-我会把这个决定留给您。
// "marker" refers to the Marker object you wish to center on
var latLng = marker.getPosition(); // returns LatLng object
map.setCenter(latLng); // setCenter takes a LatLng object
信息窗口是单独的对象,通常绑定到标记上。因此,要打开信息窗口,您可能会执行以下操作(但这取决于您的代码):
var infoWindow = marker.infoWindow; // retrieve the InfoWindow object
infoWindow.open(map); // Trigger the "open()" method
希望这可以帮到你。var marker = new google.maps.Marker({
position: new google.maps.LatLng(latitude, longitude),
title: markerTitle,
map: map,
});
google.maps.event.addListener(marker, 'click', function () {
map.panTo(marker.getPosition());
//map.setCenter(marker.getPosition()); // sets center without animation
});
panBy
,它可以帮助您按给定像素距离更改地图的中心点。 :) - Sumit Tawal这样做就可以了!
google.maps.event.trigger(map, "resize");
map.panTo(marker.getPosition());
map.setZoom(14);
你需要将你的地图设置为全局
map.setCenter(window.markersArray[2].getPosition());
所有的标记信息都在markersArray数组中,它是全局的。因此,您可以使用window.variablename从任何地方访问它。每个标记都有一个唯一的ID,您可以将该ID放在数组的键中。因此,您可以像这样创建标记:
window.markersArray[2] = new google.maps.Marker({
position: new google.maps.LatLng(23.81927, 90.362349),
map: map,
title: 'your info '
});
<ul class="locationList" ng-repeat="LocationDetail in coordinateArray| orderBy:'LocationName'">
<li>
<div ng-click="focusMarker(LocationDetail)">
<strong><div ng-bind="locationDetail.LocationName"></div></strong>
<div ng-bind="locationDetail.AddressLine"></div>
<div ng-bind="locationDetail.State"></div>
<div ng-bind="locationDetail.City"></div>
<div>
</li>
</ul>
$scope.focusMarker = function (coords) {
map.setCenter(new google.maps.LatLng(coords.Latitude, coords.Longitude));
map.setZoom(14);
}
{
"Name": "Taj Mahal",
"AddressLine": "Tajganj",
"City": "Agra",
"State": "Uttar Pradesh",
"PhoneNumber": "1234 12344",
"Latitude": "27.173891",
"Longitude": "78.042068"
}
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});