【问题标题】:google map api v3 search box with dragable marker and input box [closed]带有可拖动标记和输入框的谷歌地图 api v3 搜索框 [关闭]
【发布时间】:2015-02-05 19:35:32
【问题描述】:

我正在使用谷歌地图 api v3。我想实现这样的谷歌地图-https://developers.google.com/maps/documentation/javascript/examples/places-searchbox ,但带有可拖动的标记,将当前的 lat,lng 分别放入两个不同的输入框中。

任何帮助表示赞赏,谢谢。

【问题讨论】:

    标签: javascript jquery google-maps google-maps-api-3


    【解决方案1】:

    与您提供的链接相同的 HTML。

    我刚刚加了

    <input id="lat"> 
    <input id="lng"> 
    

    标记中的某处。

    function initialize() {
      var markers = [];
      var map = new google.maps.Map(document.getElementById('map-canvas'), {
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
      var defaultBounds = new google.maps.LatLngBounds(
          new google.maps.LatLng(-33.8902, 151.1759),
          new google.maps.LatLng(-33.8474, 151.2631));
      map.fitBounds(defaultBounds);
      var input = /** @type {HTMLInputElement} */(
        document.getElementById('pac-input')
      );
      map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
      var searchBox = new google.maps.places.SearchBox(
        /** @type {HTMLInputElement} */(input));
      google.maps.event.addListener(searchBox, 'places_changed', function() {
        var places = searchBox.getPlaces();
        if (places.length == 0) {
          return;
        }
        for (var i = 0, marker; marker = markers[i]; i++) {
          marker.setMap(null);
        }
        markers = [];
        var bounds = new google.maps.LatLngBounds();
        for (var i = 0, place; place = places[i]; i++) {
          var image = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
          };
          var marker = new google.maps.Marker({
            draggable: true,
            map: map,
            icon: image,
            title: place.name,
            position: place.geometry.location
          });
          // drag response
          google.maps.event.addListener(marker, 'dragend', function(e) {
            displayPosition(this.getPosition());
          });
          // click response
          google.maps.event.addListener(marker, 'click', function(e) {
            displayPosition(this.getPosition());
          });
          markers.push(marker);
          bounds.extend(place.geometry.location);
        }
        map.fitBounds(bounds);
      });
      google.maps.event.addListener(map, 'bounds_changed', function() {
        var bounds = map.getBounds();
        searchBox.setBounds(bounds);
      });
      // displays a position on two <input> elements
      function displayPosition(pos) {
        document.getElementById('lat').value = pos.lat();
        document.getElementById('lng').value = pos.lng();
      }
    }  
    google.maps.event.addDomListener(window, 'load', initialize);
    

    【讨论】:

    • 我找到了我要找的东西@Emmanuel,thanx
    • 我收到“未捕获的 ReferenceError: google is not defined”。我做错了什么?
    • 我在这里有一个工作代码库(不知道为什么它不在小提琴中):jsfiddle.net/Cozmoz/7guo3jyq/6 使用以下内容进行工作比较:developers.google.com/maps/documentation/javascript/examples/…
    • Cozmoz,这是我的问题吗?我认为问题是 - API 密钥在 jsfiddle 上不起作用。 - 错误的装载顺序。首先,您需要包含函数 initAutocomplete 的 js 代码。然后只能使用 ...callback=initAutocomplete 加载脚本。或者使用 ASYNC DEFER 加载谷歌地图。希望这会有所帮助
    猜你喜欢
    • 2011-08-07
    • 2015-07-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多