简体   繁体   English

jQuery-在Google Maps API中更改名称,经度和纬度位置的值

[英]JQuery - Change value of Name, Longitude, and Latitude location in google maps api

I am trying to implement a feature on my site where certain list of places are displayed next to a Map that when a user selects a place in the list it automatically sets the Name, Longitude, and Latitude on the map for the user to identify where it is. 我正在尝试在我的网站上实现某个功能,该功能会在地图旁边显示某些地点列表,当用户在列表中选择地点时,它会自动在地图上设置Name, Longitude, and Latitude ,以便用户识别在哪里它是。

Unfortunately, I got stuck in changing the value of the Name, Longitude, and Latitude in the map if the user selects a different place in the list. 不幸的是,如果用户在列表中选择其他位置,我将无法更改地图中“名称”,“经度”和“纬度”的值。

I referred to this stackoverflow question 我提到了这个stackoverflow问题

so far I have come up with this code. 到目前为止,我已经提出了这段代码。

HTML 的HTML

    <div class="col-md-12">

    <div class="col-md-3">
        <div class="list-group">
            <a href="#" class="list-group-item" data-itemtype="['Maroubra Beach', '-33.950198', '151.259302']">Cras justo odio</a>
            <a href="#" class="list-group-item" data-itemtype="['Coogee Beach', '-33.923036', '151.259052']">Dapibus ac facilisis in</a>
            <a href="#" class="list-group-item" data-itemtype="['Cronulla Beach', '-34.028249', '151.157507']">Morbi leo risus</a>
            <a href="#" class="list-group-item" data-itemtype="['Manly Beach', '-33.80010128657071', '151.28747820854187']">Porta ac consectetur ac</a>
            <a href="#" class="list-group-item" data-itemtype="['Bondi Beach', '-33.890542', '151.274856']">Vestibulum at eros</a>
        </div>
    </div>

    <div class="col-md-9">
        <div id="map"></div>
    </div>

</div>


<script src="~/Scripts/jquery-1.10.2.min.js"></script>

<script>

      // Note: This example requires that you consent to location sharing when
      // prompted by your browser. If you see the error "The Geolocation service
      // failed.", it means you probably did not give permission for the browser to
      // locate you.

    function initMap(mapLocation) {

        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            center: new google.maps.LatLng(-33.92, 151.25),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var infowindow = new google.maps.InfoWindow();

        var marker, i;

        for (i = 0; i < mapLocation; i++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(mapLocation[i][1], mapLocation[i][2]),
                map: map
            });
        }

    }

    $('.list-group-item').on('click', function (e) {

        var previous = $(this).closest(".list-group").children(".active");
        previous.removeClass('active'); // previous list-item
        $(e.target).addClass('active'); // activated list-item

        var locations = $('.list-group').find('.active').data('itemtype');

        initMap(locations);

    });

</script>
<script async defer
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB3qsbdbcouUb6qSaBxQCuiEDI03JA-zLc&callback=initMap">
</script>

please help me to be on track with the development. 请帮助我跟上发展的步伐。

Thank you in advance 先感谢您

A solution using data attributes. 使用数据属性的解决方案。 I modified your data attributes slightly because using... 我稍微修改了您的数据属性,因为使用...

data-itemtype="['Maroubra Beach', '-33.950198', '151.259302']"

...doesn't give you an array to use, just a string that looks like an array. ...不会给您使用数组,只是一个看起来像数组的字符串。

HTML: HTML:

<div class="container">
 <div class="row">
  <div class="list-group col-sm-2">
          <a id="0" href="#" class="list-group-item" data-name='Maroubra Beach' data-lat='-33.950198' data-lng='151.259302'>Cras justo odio</a><br />
          <a id="1" href="#" class="list-group-item" data-name='Coogee Beach' data-lat='-33.923036' data-lng='151.259052'>Dapibus ac facilisis in</a><br />
          <a id="3" href="#" class="list-group-item" data-name='Cronulla Beach' data-lat='-34.028249' data-lng='151.157507'>Morbi leo risus</a>
  </div>
  <div class="col-sm-10">    
      <div id="map_canvas"></div>
  </div>
 </div>
</div>

Javacript: Javacript:

(function(Mapping, $, undefined) {
    var self = this;

    function Initialize() {
        var myOptions = {
          zoom: 10,
            center: new google.maps.LatLng(-33.92, 151.25),
            mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      self.map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
      self.infoWindow = new google.maps.InfoWindow();

      $('.list-group-item').each(function() {
        var $this = $(this);
        var pos = new google.maps.LatLng($this.data('lat'), $this.data('lng'));
        var marker = new google.maps.Marker({
                        position: pos,
                        map: self.map
                      });

        $this.click(function() {
            self.map.panTo(pos);
            self.infoWindow.setContent($this.data('name'));
            self.infoWindow.open(self.map, marker);
            $this.siblings().removeClass('active');
            $this.addClass('active');
        });
      });
  }

  Initialize();
})(window.Mapping = window.Mapping || {}, jQuery);

JSFiddle JSFiddle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Maps JS API - 查找给定位置的位置名称(纬度/经度) - Google Maps JS API - Find name of place given location (latitude/longitude) 使用Google自动填充API根据位置名称获取纬度和经度 - Get latitude and longitude based on location name with Google Autocomplete API 使用Google Maps API填充经度和纬度 - Populate the longitude and latitude using google maps API 更改经纬度Google Maps API - Changing latitude and longitude google maps api 使用google maps api为经度和纬度使用变量 - Using variables for longitude and latitude with google maps api Google Maps API按经度/纬度删除标记 - Google Maps API remove marker by latitude/longitude 是否可以在javascript函数中从具有纬度和经度的var在Google地图中创建位置名称为var的变量? - Is it possible in a javascript function, to create from var with latitude and longitude of a position in Google maps a var with location name? 如何在不重新加载页面的情况下使用经度和纬度坐标动态更改Google地图上的位置点 - How to change location point on google maps dynamically using longitude and latitude coordinates without reload the page 给定带有位置“ loc”搜索的链接,即可使用Google Maps API从标记中提取纬度和经度 - Given a link with a location “loc” search extract latitude and longitude from marker using Google Maps API 显示位置使用经度/纬度坐标 - Google地图 - Display location Using Longitude/Latitude Coordinates - Google Maps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM