谷歌地图 V3 将中心设置为特定标记 [英] Google map V3 Set Center to specific Marker

查看:15
本文介绍了谷歌地图 V3 将中心设置为特定标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google Map V3 显示从数据库中提取的标记列表,使用 MYsql 和 PHP.我的标记是使用完整地址(来自 DB,包括邮政编码)在地图上设置的,因为我没有 Long,Lat 信息一切正常,我的循环如下

I am using Google Map V3 to display a list of markers pulled from a DB, using MYsql and PHP. MY markers are set on the map using the full address (from DB, including postcode) as I don't have the Long,Lat info All is working fine, MY loop looks as follow

while...
Addmarker(map,'MY ADdress','Title');
end while;

现在我想将地图设置为循环中包含的特定标记,该标记将匹配之前输入的邮政编码.如何将地图设置为以该标记为中心并打开信息窗口?

Now I would like to set the map to a specific marker contained in the loop that will match a previously entered postcode. How can I set the map to center to this marker and open the infowindow?

推荐答案

在地图上有标记后,您可以通过 API 检索纬度/经度坐标并使用它来设置地图的中心.您首先只需要确定您希望以哪个标记为中心 - 我将把它留给您.

Once you have markers on the map, you can retrieve the Lat/Long coordinates through the API and use this to set the map's center. You'll first just need to determine which marker you wish to center on - I'll leave that up to you.

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

信息窗口是单独的对象,通常绑定到标记,因此要打开信息窗口,您可能会执行以下操作(但这取决于您的代码):

Info windows are separate objects which are typically bound to a marker, so to open the info window you might do something like this (however it will depend on your code):

var infoWindow = marker.infoWindow; // retrieve the InfoWindow object
infoWindow.open(map); // Trigger the "open()" method

希望这会有所帮助.

这篇关于谷歌地图 V3 将中心设置为特定标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆