【问题标题】:google map - mark section after click pin谷歌地图 - 点击别针后标记部分
【发布时间】:2018-04-11 09:35:01
【问题描述】:

我的页面有 2 个面 -

一方面我有部分。每一个都是包含文本和位置的旅行中的一天。另一方面,我有一个谷歌地图,上面有参考部分的图钉。

我希望允许用户单击图钉,页面将滚动到相关部分,并使用背景颜色绘制该部分。

Ans 也相反 - 当用户点击某个部分时,相关的 pin 会改变他的颜色。

我不知道如何使用 Google-Maps...

这是我的脚本代码:

<!-- Google map -->
    <script src="https://maps.googleapis.com/maps/api/js?key=fUA&callback=initMap" type="text/javascript"></script>
    <script type="text/javascript">
     var markers = [

            {
                "title": "Praho",
                "lat": "50.075538",
                "lng": "14.437801",
                "description": "Day: 1"
            }           
         , 
            {
                "title": "Český ráj",
                "lat": "50.586675",
                "lng": "15.157302",
                "description": "Day: 2"
            }           
         , 
            {
                "title": "Bešeňová",
                "lat": "49.100211",
                "lng": "19.434720",
                "description": "Day: 3"
            }           
            ];
    var infoWindows = []; // for open/close button

    var mapOptions = {
        center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP // HYBRID
    };
    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);   

    window.onload = function () {

        var infoWindow = new google.maps.InfoWindow();
        var lat_lng = new Array();
        var latlngbounds = new google.maps.LatLngBounds();
        for (i = 0; i < markers.length; i++) {
            var data = markers[i]
            var myLatlng = new google.maps.LatLng(data.lat, data.lng);
            lat_lng.push(myLatlng);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: data.title
            });

            latlngbounds.extend(marker.position);

            // open popup by click
            (function (marker, data) {
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent(data.description);
                    infoWindow.open(map, marker);
                });
            })(marker, data);
            //


            // Open all popups
            var infowindow = new google.maps.InfoWindow({
                content: '<div id="iw-container">' +'<div class="iw-title">' + data.description + '</div>' + '</div>',
                maxWidth: 350
            });

        /*  google.maps.event.addListener(marker, 'mouseover', function () {
                infowindow.open(map, marker);
            });
        */  
            infowindow.open(map, marker);               
            //
            //Open/close infoWindow - Store all marker and infowindow in JSON array...
            var dict_map = {};
            dict_map['infoWinObj'] = infowindow;
            dict_map['markerObj'] = marker;

            //push JSON dict in array
            infoWindows.push(dict_map);               
            //  
        }
        map.setCenter(latlngbounds.getCenter());
    //  map.fitBounds(latlngbounds);

        //***********ROUTING****************//

        //Initialize the Path Array
        var path = new google.maps.MVCArray();

        //Initialize the Direction Service
        var service = new google.maps.DirectionsService();

        //Set the Path Stroke Color
        var poly = new google.maps.Polyline({ map: map, strokeColor: '#4986E7' });

    }

    function closeAllInfoWindows()
    {
        for (var i=0;i<infoWindows.length;i++) {
           if (infoWindows[i]['infoWinObj'])
              infoWindows[i]['infoWinObj'].close();
        }
    } 

    function openAllInfoWindows()
    {
        for (var i=0;i<infoWindows.length;i++) {                   
              infoWindows[i]['infoWinObj'].open(map, infoWindows[i]['markerObj']);
        }
    }   

    </script>

【问题讨论】:

    标签: javascript jquery google-maps


    【解决方案1】:

    我刚刚做了一个基本的演示。

    主要思想是panTo() 标记和scrollTop() 到您单击的日期块。

    编辑:在 DayInfo 数组中包含倾斜和内容。现在您可以在 infowindow 中构建您想要的内容。

    var map;
    var StartPoint = new google.maps.LatLng(50, 10);
    
    var DayInfo = [{
        ID: "day1",
        Title: "Title1",
        Content: "Content1",
        Location: new google.maps.LatLng(51.72977747627091, 6.605224609375),
      },
      {
        ID: "day2",
        Title: "Title2",
        Content: "Content2",
        Location: new google.maps.LatLng(52.78551578550006, 13.460693359375),
      },
      {
        ID: "day3",
        Title: "Title3",
        Content: "Content3",
        Location: new google.maps.LatLng(50.292175137097736, 14.32861328125),
      },
      {
        ID: "day4",
        Title: "Title4",
        Content: "Content4",
        Location: StartPoint
      }
    ]
    
    var DaysMarker = [];
    
    function initialize() {
      var mapOptions = {
        center: StartPoint,
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
    
      map = new google.maps.Map(document.getElementById("map"), mapOptions);
    
      DayInfo.forEach(function(dayInfo, index) {
        var marker = new google.maps.Marker({
          position: dayInfo.Location,
          map: map,
          isOpended: false,
    
          Open: function() {
            if (!this.isOpended) {
              this.infowindow.open(map, marker)
              this.isOpended = true
            }
            map.panTo(this.position)
          }
        })
    
        marker.infowindow = new google.maps.InfoWindow({
          content: '<div id="iw-container">' +
            '<h1>' + dayInfo.Title + '</h1>' +
            '<div class="iw-title">' +
            'Day' + (index + 1) + '<br>' +
            dayInfo.Content +
            '</div>' + '</div>',
          maxWidth: 350
        });
    
        marker.addListener('click', function() {
          if (this.isOpended) {
            this.infowindow.close()
            this.isOpended = false
          } else {
            this.Open()
            // var offsetTop = $('.day').get(index).offsetTop;
            $('html, body').animate({
              // scrollTop: offsetTop
              scrollTop: $('#' + dayInfo.ID).offset().top
            }, '500');
          }
        })
    
        DaysMarker.push(marker)
      })
    }
    
    $(function() {
      initialize()
    
      $('.day').on('click', function() {
        var index = $(this).index();
        DaysMarker[index].Open()
        // var offsetTop = $('.day').get(index).offsetTop;
        $('html, body').animate({
          // scrollTop: offsetTop
          scrollTop: $(this).offset().top
        }, '500');
      })
    })
    * {
      padding: 0;
      margin: 0;
    }
    
    body>div {
      position: relative;
      height: 100vh;
      vertical-align: top;
    }
    
    #map {
      width: 70%;
      position: fixed !important;
      right: 0;
      top: 0;
    }
    
    #days {
      margin-left: -4px;
      width: 30%;
      display: inline-block;
    }
    
    .day {
      height: 100vh;
      outline: 1px blue solid;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    
    
    <div id="days">
      <div id="day1" class="day">Day1</div>
      <div id="day2" class="day">Day2</div>
      <div id="day3" class="day">Day3</div>
      <div id="day4" class="day">Day4</div>
    </div>
    <div id="map"></div>

    【讨论】:

    • 谢谢,但是每个图钉的标题/描述呢?
    • 这是一个展示如何设置标题和描述的示例。 Google Map API Doc
    • 但在我的代码中,我为数组添加了标题 - `var markers = [...]`。是否可以为您的标题添加标题 - var Locations = [...]
    • 我刚刚根据您的评论要求编辑了帖子。
    • 非常感谢!如果我能做的不仅仅是最后一件事 - 在你的代码中,当我点击 pin 时,页面滚动到右侧的 div。您为此使用“索引”。这是问候,但在我的代码中,我给每个 div/section 唯一的 ID。我可以在 var DayInfo = [..] 数组中实现这个 ID 并使用 in 来标记滚动锚点吗?
    猜你喜欢
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 2012-12-04
    相关资源
    最近更新 更多