Google 地图 V3 可见标记上的 fitBounds

2024-03-12

发现很难获得关于此的清晰信息,但我想要实现的是可见标记上的 fitBounds。

该数组定义标题、类别、纬度/经度和 href。它将所有标记添加到地图,但仅显示第一个类别。当您单击其他类别时,它也会显示这些类别。

我可以让 fitBounds 处理其他较小的示例,其中它只需要在初始加载时适合 Bounds,但现在我希望每次显示/隐藏标记时都适合 fitBounds。

任何建议表示赞赏。

function initialize() {
            var markers = new Array();
            var locations = [
                ['Ness Country Park', '', 'walking', 54.933402, -7.197418, "/local-activities/specific/ness-country-park"],
                ['Learmount Forest', '', 'walking', 54.862672, -7.085495, "/local-activities/specific/learmount-forest"],
                ['Muff Glen', 'Muff Glen Forest is nestled in a valley, just outside Eglinton. ', 'walking', 55.029472, -7.170124, "/local-activities/specific/muff-glen"],
                ['Banagher Forest', '', 'walking', 54.893855, -7.020693, "/local-activities/specific/banagher-forest"],
                ['Binevenagh Forest', '', 'walking', 55.083549, -6.901131, "/local-activities/specific/binevenagh-forest"],
                ['17th Century Walls', '', 'explore', 54.997666, -7.319770, "/local-activities/specific/17th-century-walls"],
                ['Apprentice Boys Memorial Hall', '', 'explore', 54.995339, -7.323890, "/local-activities/specific/apprentice-boys-memorial-hall"],
                ['The Bogside Artists', '', 'explore', 54.998466, -7.325177, "/local-activities/specific/the-bogside-artists"],
                ['Foyle Valley Railway Museum', '', 'explore', 54.991154, -7.321572, "/local-activities/specific/foyle-valley-railway-museum"],
                ['Gasyard Heritage Centre', '', 'explore', 54.989923, -7.333674, "/local-activities/specific/gasyard-heritage-centre"],
                ['The Guildhall', '', 'explore', 54.996053, -7.320800, "/local-activities/specific/the-guildhall"],
                ['Harbour Museum', '', 'explore', 54.998269, -7.319427, "/local-activities/specific/harbour-museum"],
                ['Museum of Free Derry', '', 'explore', 54.997099, -7.326121, "/local-activities/specific/museum-of-free-derry"],
                ['Riverwatch Aquarium and Visitor Centre', '', 'explore', 54.988950, -7.317538, "/local-activities/specific/riverwatch-aquarium-and-visitor-centre"],
                ['St Augustines Church', '', 'explore', 54.994736, -7.324040, "/local-activities/specific/st-augustines-church"],
                ['St Columbs Cathedral', '', 'explore', 54.994564, -7.322345, "/local-activities/specific/st-columbs-cathedral"],
                ['St Columbas Church Tower', '', 'explore', 55.010132, -7.330992, "/local-activities/specific/st-columbas-church-tower"],
                ['Tower Museum', '', 'explore', 54.997112, -7.320328, "/local-activities/specific/tower-museum"],
                ['Workhouse Museum', '', 'explore', 54.994995, -7.305071, "/local-activities/specific/workhouse-museum"],
                ['Hezlett House', '', 'explore', 55.161697, -6.787148, "/local-activities/specific/hezlett-house"],
                ['Mussenden Temple & Downhill Demense', '', 'explore', 55.166404, -6.819849, "/local-activities/specific/mussenden-temple-and-downhill-demense"],
                ['Springhill House', '', 'explore', 54.685615, -6.656857, "/local-activities/specific/springhill-house"],
                ['Fort Dunree', '', 'explore', 55.196691, -7.547607, "/local-activities/specific/fort-dunree"],
                ['Barron Top Fun Farm', '', 'explore', 54.856497, -7.335434, "/local-activities/specific/barron-top-fun-farm"],
                ['Ulster American Folk Park', '', 'explore', 54.587973, -7.377191, "/local-activities/specific/ulster-american-folk-park"],
                ['Wellbrook Beetling Mill', '', 'explore', 54.651366, -6.836071, "/local-activities/specific/wellbrook-beetling-mill"],
                ['Claudy Country Park', '', 'cycling', 54.911868, -7.151756, "/local-activities/specific/claudy-country-park"],
                ['Faughan Valley Cycle Route', '', 'cycling', 54.981797, -7.337022, "/local-activities/specific/faughan-valley-cycle-route"],
                ['Foyle Valley Cycle Route ', '', 'cycling', 55.001530, -7.321208, "/local-activities/specific/foyle-valley-cycle-route-"],
                ['Royal Portrush Golf Club', '', 'golfing', 55.199823, -6.634820, "/local-activities/specific/royal-portrush-golf-club"],
                ['Porstewart Golf Club', '', 'golfing', 55.172149, -6.724534, "/local-activities/specific/porstewart-golf-club"],
                ['Castlerock Golf Club ', '', 'golfing', 55.165945, -6.785645, "/local-activities/specific/castlerock-golf-club-"],
                ['Cregan Country Park', '', 'family', 54.999841, -7.343888, "/local-activities/specific/cregan-country-park"],
                ['Riverwatch Aquarium', '', 'family', 54.988852, -7.317495, "/local-activities/specific/riverwatch-aquarium"],
                ['Far & Wild Adventure Activities', '', 'family', 55.048369, -7.276855, "/local-activities/specific/far-and-wild-adventure-activities"],
                ['Brunswick Cinebowl', '', 'family', 55.013008, -7.324448, "/local-activities/specific/brunswick-cinebowl"],
                ['Campsie Karting', '', 'family', 55.036076, -7.196903, "/local-activities/specific/campsie-karting"],
                ['Barron Top Fun Farm', '', 'family', 54.856497, -7.335434, "/local-activities/specific/barron-top-fun-farm-1"],
                ['Ulster American Folk Park', '', 'family', 54.587973, -7.377191, "/local-activities/specific/ulster-american-folk-park-1"],
                ['Giants Causeway', '', 'family', 55.233317, -6.518154, "/local-activities/specific/giants-causeway"],
                ['Carick-a-Rede Rope Bridge', '', 'family', 55.237282, -6.351643, "/local-activities/specific/carick-a-rede-rope-bridge"],
                ['Mussenden Temple', '', 'family', 55.165410, -6.818218, "/local-activities/specific/mussenden-temple"]
            ];

            //Set up Google Map
            var map = new google.maps.Map(document.getElementById('map-canvas'), {
                zoom: 15,
                center: new google.maps.LatLng(55.013431, -7.314148),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });


            //InfoBox will be used so InfoWindow is not needed
            //var infowindow = new google.maps.InfoWindow();

            //Set shadow options
            var shadow = new google.maps.MarkerImage('images/marker-shadow.png',
                new google.maps.Size(77.0, 62.0),
                new google.maps.Point(0, 0),
                new google.maps.Point(22.0, 62.0)
            );
            var marker, i;



            for ( var i = 0; i < locations.length; i++ ) {
              console.log(locations[i][3], locations[i][4]);
            }


            for (i = 0; i < locations.length; i++) {  
                marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][3], locations[i][4]),
                map: map,
                shadow: shadow,
                icon: 'images/map_'+locations[i][2]+'_image.png'
            });

            markers.push(marker);

            //Create div for InfoBox content
            var boxText = document.createElement("div");

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function () {
                    //Style InfoBox content
                    boxText.style.cssText = "border: none 0; margin-top: 8px; background:#fff; padding: 8px;border:1px solid #877856;";
                    boxText.innerHTML = locations[i][0]+"<br />"+locations[i][2]+"<br />"+locations[i][1]+"<img src='images/tipbox.png' class='infobox-arrow' />";

                    //Remove close box bug fix
                    $(".infobox-close").remove();

                    //Set InfoBox options
                    var myOptions = {
                        content: boxText
                        ,disableAutoPan: false
                        ,maxWidth: 0
                        ,pixelOffset: new google.maps.Size(-140, -180)
                        ,zIndex: null
                        ,boxStyle: { 
                            opacity: 1
                            ,width: "280px"
                            }
                        ,closeBoxMargin: "12px 7px 0 0"
                        ,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
                        ,infoBoxClearance: new google.maps.Size(1, 1)
                        ,isHidden: false
                        ,pane: "floatPane"
                        ,enableEventPropagation: false
                    };

                    //Move map to center on marker
                    map.setCenter(marker.getPosition());

                    //Attach info box to relevant marker
                    var ib = new InfoBox(myOptions);
                    ib.open(map, marker);

                    //Close InfoBox when map tiles are clicked
                    $("canvas").click(function () {
                        ib.close(map, marker);
                    });
                }
                })(marker, i));
            };


            //shows all markers of a particular category, and ensures the checkbox is checked
            function show(category) {
                for (var i=0; i<locations.length; i++) {
                    if (locations[i][2] == category) {
                        markers[i].setVisible(true);
                    }
                }
            }

            //hides all markers of a particular category, and ensures the checkbox is cleared
            function hide(category) {
                for (var i=0; i<locations.length; i++) {
                    if (locations[i][2] == category) {
                        markers[i].setVisible(false);
                    }
                }
            }

            //show or hide the categories initially
            show("walking");
            hide("golfing");
            hide("family");
            hide("explore");
            hide("cycling");


            //Show/Hide category markers
            $("#activities .checkbox").click(function(){
                var cat = $(this).attr("value");
                // If checked
                if ($(this).is(":checked"))
                {
                    show(cat);

                }
                else
                {
                    hide(cat);
                }
            });
        };

        //Execute function onload
        window.load = initialize();

好吧,你可以在你的内部进行编程show函数,我为您创建了自己的小提琴,它演示了如何设置从 3 个标记的数组中可见的 2 个标记,并仅针对这些标记设置 fitBounds:

小提琴的例子 http://jsfiddle.net/22qte/113/

由于代码现在可见,根据您的情况,您可以单独制作fitBoundsToVisibleMarkers()始终在之后运行的方法show() and hide()方法。例如:

function fitBoundsToVisibleMarkers() {

    var bounds = new google.maps.LatLngBounds();

    for (var i=0; i<markers.length; i++) {
        if(markers[i].getVisible()) {
            bounds.extend( markers[i].getPosition() );
        }
    }

    map.fitBounds(bounds);

}

你的代码执行以下操作:

function show(category) {
    for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == category) {
            markers[i].setVisible(true);
        }
    }

    // updating bounds in view
    fitBoundsToVisibleMarkers();
}

function hide(category) {
    for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == category) {
            markers[i].setVisible(false);
        }
    }

    // updating bounds in view
    fitBoundsToVisibleMarkers();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google 地图 V3 可见标记上的 fitBounds 的相关文章

  • 谷歌地图破坏 KML

    我已经创建了一些世界地区的 KML 文件 以便使用 Google 地图 API 进行显示 这一直工作正常 但最近有一个区域开始显示数据好像已损坏 你可以在这里明白我的意思 https drive google com file d 0B9u
  • 如何在 googlemaps url 中设置航点

    我有一个带有地图的网站 我有一个文本字段 在其中输入我想要前往已设置的特定目的地的城市名称 为了到达这个目的地 我在代码中硬编码了路径点 所有这些都很好用 我现在有一个按钮 单击该按钮会给我指示 文本格式 当我单击另一个按钮时 我会得到一个
  • Google 地图 v3 与 MeteorJS 加载同步问题

    我的应用程序在 MeteorJS 框架上运行并使用谷歌地图 javascript api v3 谷歌地图加载方案类似于此中解释的方案post https stackoverflow com a 16797219 942899 和官方的很相似
  • JS Maps v3:带有用户个人资料图片的自定义标记

    两天以来 我一直在努力解决一些我认为很简单的事情 在地图上 我必须为每个用户显示一个标记 其中包含用户 FB 个人资料图片 我想知道如何才能得到与此类似的结果 我所尝试的确实很hackish 我把FB图片作为标记图标 我在标记的标签上放置了
  • ... 不支持谷歌地图 Javascript API。使用其他浏览器

    我正在使用两个不同的第三方程序 不同的公司 它们显然使用 Google Maps Javascript API 当他们加载地图时 会显示世界地图 但会显示一条错误消息 Der von Ihnen verwendete Browser wir
  • 谷歌地图上 100k 或更多标记,无需聚类

    我有一些通过安装在汽车上的 GPS 设备收集的数据 所以我拥有的数据基本上都位于街道 道路上 周围 每个坐标都有一些值 数据的格式是这样的 lat long value 12 979155 77 644925 6 12 97916833 7
  • Google Maps API v3:单击 DOM 元素时关闭信息窗口

    我是第一次使用 Google 地图 所以我在 CSS Tricks 上查看了一个很好的教程 http css tricks com google maps slider http css tricks com google maps sli
  • 多边形来自折线?

    我正在尝试自动从折线创建多边形 到目前为止 我一直在正确计算折线每个部分的外推边 条件 基线和边之间的距离是常数 如何从基点 红色点 计算边 蓝色点 的角点 这是我在 Qt 中的代码 这对我来说很好用 QPolygonF projectPL
  • 使用 PHP json_encode() 和 MySQL 返回 JSON 对象以传递给 jQuery 函数 [重复]

    这个问题在这里已经有答案了 我正在尝试从 MySQL 结果创建一个 json 对象 但没有得到我需要的结果 这是 PHP json array result mysqli query connection query echo while
  • 谷歌地理编码 API 不如使用地址的谷歌地图准确

    我有很多例子 其中谷歌地图返回确切的位置 而地理编码无法找到精确的匹配 并且仅提供紧密匹配的纬度 经度 这对我的目的没有好处 例如 谷歌地图中的 1729 State Road 8 auburn IN 46706 将我精确地定位到了沃尔玛配
  • 通过google地图获取我当前位置10公里半径范围内所有位置的纬度和经度(使用PHP)

    我对谷歌地图很陌生 我有一个表格 其中列出了所有位置 超级商店的位置 及其纬度和经度 现在我想知道这些超级商店在我当前位置 10 公里半径范围内的所有可能位置 可能是纬度和经度 我不知道如何使用 Google 地图 在 php 代码中 来做
  • Google 地图:挪威邮政编码未返回任何结果

    简而言之 我们有一些挪威邮政编码 并正在使用 API 来获取它们的地址以及经纬度 没什么高雅的 但在大约 10 的邮政编码上 API 不返回任何结果 下面是一个示例 成功为postal code 1151 http maps googlea
  • 从数据层中删除所有特征

    我用过类似的东西 var map function initialize map new google maps Map document getElementById map canvas zoom 4 center lat 28 lng
  • 将声音添加到标记数组 - 谷歌地图 javascript

    我是新来的 所以我知道我没有任何可信度 我是一名艺术家 对编程很陌生 所以我理解是否没有人会承担这个任务 我发布这篇文章是因为这是一个简单的问题 S 这是创建多个标记的代码 主要来自谷歌开发者网站 它工作正常并为每个标记创建一个自定义图标
  • Google 放置 API:从 CID 到参考?

    我的目标 用已知的商业地点填充数据库 以便生成包含这些地点的地图 我坚持使用 已知地点 因为我的用户只会搜索数据库中的地点 我不想在地图上重新创建商业地点作为标记 因此纬度和经度不足以识别地点 因为这些地点已经在 Google 地图上提供了
  • 导致链接平移并打开地图中的标记

    JSFiddle http jsfiddle net megatimes NVDLf 7 http jsfiddle net megatimes NVDLf 7 我有一张地图 它从数组创建多个标记 地图下方是一些链接 单击这些链接时 我想让
  • 如何使用 Google Direction api 或 iPhone 应用程序的其他一些 api 比较两条路线

    我想比较两条路线以检查它们在我的 iPhone 应用程序中是否相同 有一个人X想要从A点到B点 另一个人想要从A1点到B1点 我可以使用谷歌的方向 API 获取 A 到 B 之间的路线 http maps googleapis com ma
  • google.maps.Geocoder.geocode() Geometry.location lat/lng 属性名称经常更改

    我有一个应用程序 我正在使用 Google Javascript 地理编码 API 来获取地址的纬度 经度 代码是这样的 geocoder new google maps Geocoder geocoder geocode address
  • Google 地图第二次无法加载 - AngularJS

    我正在使用 GoogleMap API angular google maps js 包 并且我有一个非常奇怪的行为 The first time I load it i get the full map loaded like here
  • 有没有办法限制 Google 地点自动完成功能搜索城市的街道?

    使用 Google 地方信息时可以将搜索限制在城市的街道吗Autocomplete 您可以设置Autocomplete使用选项geocode作为类型 这将限制返回到地址的结果 var input document getElementByI

随机推荐

  • DrawerLayout 和多窗格布局

    我的应用程序使用多窗格布局 http developer android com design patterns multi pane layouts html显示作业列表 每个Assignment可以放在一个AssignmentCateg
  • 我的自动装配数据源在 Spring 中返回 null,这是怎么回事?

    我正在尝试设置我的 dataSource 以获取连接 但它在 conn dataSource getConnection 上返回 null 这是我的 DAO 中的相关代码 Autowired public void setDataSourc
  • 无法登录部署在 Tomcat7 上的 Apache Usergrid

    我在 Ubuntu 14 04 LTS 上下载并构建了 Apache Usergrid 并按照以下给出的说明在本地 tomcat7 服务器上部署了 ROOT warhttps usergrid incubator apache org do
  • Bittorrent:为什么peers字段的值是二进制的,而不是Bencoded列表?

    我正在尝试用 C 语言实现 Bittorrent 首先 在编写代码片段之前 我尝试使用网络浏览器将以下消息 URL 发送到跟踪器服务器 你可以试试这个网址 http torrent ubuntu com 6969 announce info
  • 如何使用 Spark Scala 将 CSV 行拆分为元组

    这是我想通过 Scala 检索的数据 数据如下 用户ID 电影ID 1 1172 1 1405 1 2193 1 2968 2 52 2 144 2 248 首先我想跳过第一行 然后通过 split 分割用户和电影并映射到 userID m
  • 如何从 UITestControl 获取 ItemStatus?

    The UI自动化框架 http msdn microsoft com en us library ms747327 aspx有一个基类 自动化元件 http msdn microsoft com en us library system
  • md5(file_contents_as_string) 等于 md5_file(/path/to/file) 吗?

    If I do 这将始终产生与以下内容相同的哈希值 是的 他们返回相同的 var dump md5 file get contents FILE var dump md5 file FILE 在我的例子中返回这个 string 32 4d2
  • 在没有注释的情况下将 Hibernate 迁移到 JPA

    我有一个大型的非 Java EE 基于 JSF 的 Web 应用程序项目 我们的系统是分层的 在源代码意义上 有一个数据模型包 其上构建的是 DAO 包 我们在 DAO 包中专门使用 Hibernate 的 XML 配置映射 我们确实不想用
  • 外键和模型继承哪个更好?

    我有这样的用例场景 有些地方是游乐场 餐馆 剧院 酒吧 相同place可以有游乐场 餐馆 剧院等 有几种实现方法 使用外键 class Place models Model name models CharField max length
  • 如何检查连接到 GRPC 服务器的客户端

    为了为我的 GRPC 服务器 客户端设置提供更好的调试信息 我正在尝试寻找一个 APIgrpc server这使我能够检查哪些客户端连接到服务器 我发现的最有希望的问题是问题 它给出了如何在 Java GRPC 中执行此操作的起点 http
  • 使用java的X509序列号

    我需要从 X509 证书获取一些数据 如果我在 Windows 中打开证书文件 它会以这种格式显示其序列号 ex 39 65 70 eb d8 9f 28 20 4e c2 a0 6b 98 48 31 0d 我试图使用 java 获取相同
  • 修复了背景附件在 Chrome 中不起作用的问题

    我尝试在 google 上寻求帮助 这似乎是 chrome 中的一个错误 但我仍然找不到解决方案 我已经查看了许多与此类似的 stackoverflow 问题 但这些解决方案仍然没有帮助 当我尝试在页面上向下滚动时 背景图像会闪烁并变得混乱
  • Matlab:bar 不存在公共属性 CData

    Matlab示例代码无法在Matlab 2017a Linux 64位 中运行 https cn mathworks com help matlab ref bar html https cn mathworks com help matl
  • 在堆栈上增长数组

    这本质上是我的问题 在函数的生命周期中 我生成一些整数 然后在也是同一函数一部分的算法中使用整数数组 整数数组仅在函数内使用 因此将数组存储在堆栈上自然是有意义的 问题是在生成所有整数之前我不知道数组的大小 我知道如何在堆栈上分配固定大小和
  • 从另一个 url 加载动态模块 - angular4

    是否可以引用模块 已以 umd 或 es 格式编译 并将其动态加载到已编译的角度应用程序中 主 shell 应用程序托管于 http plugin shell mydomain com http plugin shell mydomain
  • 在没有数据库或用户名的情况下使用 php 密码保护文件夹/页面的最佳方法是什么

    使用没有数据库或用户名但使用 php 密码保护文件夹的最佳方法是什么 基本上 我有一个页面将列出组织的联系人 并且需要密码保护该文件夹 而无需为每个用户提供帐户 只需一个密码 该密码会经常更改并分发给群组 我知道它不是很安全 但我仍然想知道
  • ARM 未定义指令错误

    我在运行嵌入式系统 无协处理器 无 MMU Atmel 9263 时收到未定义指令错误 嵌入式系统的内存范围为 0x20000000 0x23FFFFFF 目前为止我遇到过两个案例 SP 0x0030B840 LR 2000AE78 LR指
  • 何时在 Gradle 任务中使用“<<”

    有时我会看到 task hey lt lt println Hello Gradle 其他时候我看到 task hey println Hello Gradle 什么时候使用 lt lt 你什么时候不这样做 以及为什么 lt lt 是简写d
  • 如何使用 Linq.Expressions 查询集合

    我构建了一个自定义 IQueryable 提供程序 例如 提供者转换查询 c PurchaseDate new DateTime 2011 11 29 c Name Elizabeth Brown 从底层代码到 System Linq Ex
  • Google 地图 V3 可见标记上的 fitBounds

    发现很难获得关于此的清晰信息 但我想要实现的是可见标记上的 fitBounds 该数组定义标题 类别 纬度 经度和 href 它将所有标记添加到地图 但仅显示第一个类别 当您单击其他类别时 它也会显示这些类别 我可以让 fitBounds