1

实际上,我在一个用户可以添加他的业务的网站上工作。我想给他一个在地图上添加任何位置的功能。当他保存他的业务详细信息以及该地图详细信息时,应该进入数据库,以便在将他的业务发布到网站上之后,它应该在地图上显示该位置。简而言之,我想允许用户在地图上添加他的位置,并且在网站上,该特定用户地图应该在地图上显示他的位置。由于我对这一切都不熟悉,所以我很困惑如何做到这一点。所以请帮帮我。我在谷歌上搜索了它,但我的问题没有得到任何帮助。提前致谢....

这是我尝试过的代码:

        <script type="text/javascript">
    function initialize() {
      var mapOptions = {
        center: new google.maps.LatLng(-33.8688, 151.2195),
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

      var input = /** @type {HTMLInputElement} */(document.getElementById('searchTextField'));
      var autocomplete = new google.maps.places.Autocomplete(input);

      autocomplete.bindTo('bounds', map);

      var infowindow = new google.maps.InfoWindow();
      var marker = new google.maps.Marker({
        map: map
      });

      google.maps.event.addListener(autocomplete, 'place_changed', function() {
        infowindow.close();
        marker.setVisible(false);
        input.className = '';
        var place = autocomplete.getPlace();
        if (!place.geometry) {
          // Inform the user that the place was not found and return.
          input.className = 'notfound';
          return;
        }

        // If the place has a geometry, then present it on a map.
        if (place.geometry.viewport) {
          map.fitBounds(place.geometry.viewport);
        } else {
          map.setCenter(place.geometry.location);
          map.setZoom(17);  // Why 17? Because it looks good.
        }
        marker.setIcon(/** @type {google.maps.Icon} */({
          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(35, 35)
        }));
        marker.setPosition(place.geometry.location);
        marker.setVisible(true);

        var address = '';
        if (place.address_components) {
          address = [
            (place.address_components[0] && place.address_components[0].short_name || ''),
            (place.address_components[1] && place.address_components[1].short_name || ''),
            (place.address_components[2] && place.address_components[2].short_name || '')
          ].join(' ');
        }
        var place = autocomplete.getPlace();
                document.getElementById('city2').value = place.name;
                document.getElementById('cityLat').value = place.geometry.location.lat();
                document.getElementById('cityLng').value = place.geometry.location.lng();
    var latitude = place.geometry.location.lat();
    var longitude = place.geometry.location.lng(); 
        infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
        infowindow.open(map, marker);
      });

      // Sets a listener on a radio button to change the filter type on Places
      // Autocomplete.
      function setupClickListener(id, types) {
        var radioButton = document.getElementById(id);
        google.maps.event.addDomListener(radioButton, 'click', function() {
          autocomplete.setTypes(types);
        });
      }

      setupClickListener('changetype-all', []);
      setupClickListener('changetype-establishment', ['establishment']);
      setupClickListener('changetype-geocode', ['geocode']);
    }

    google.maps.event.addDomListener(window, 'load', initialize);

        </script>
    <style>
    #map-canvas, #map_canvas {
      height: 400px;
      width: 900px;
    }

    @media print {
      #map-canvas, #map_canvas {
        height: 400px;
        width: 900px;
      }
    }

    </style>
    </head>
    <body>
    <?php
    /*
     * Smarty plugin
     * -------------------------------------------------------------
     * File:     function.map.php
     * Type:     map
     * Name:     Map
     * Purpose:  Accept the keyword & find it on a map.
     * -------------------------------------------------------------
     */

     function smarty_function_map($params, &$smarty)
    {
        ?>

        <div id="panel">
        <label>Location/Area*</label><input id="searchTextField" type="text" size="50" placeholder="Enter Location/Area">
        <input type="text" id="city2" name="city2" />
        <input type="text" id="cityLat" name="cityLat" />
        <input type="text" id="cityLng" name="cityLng" />
        </div>
        <div id="map-canvas"></div>

        <?php

    }
    ?>
    </body>
    </html>
4

1 回答 1

2

要允许用户通过单击向地图添加标记,您需要设置一个事件侦听器来侦听地图上的任何点击。例子:

var marker;

google.maps.event.addListener(map, 'click', function(event) {
  if ( marker ) {
    marker.setPosition(event.latLng);
  } else {
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map,
      draggable:true
    });
  }
});

您还需要允许用户在绘制标记后拖动它。您可以通过将标记设置为可拖动来做到这一点(我在上面的示例中这样做了)。您还需要通过将事件侦听器附加到相关标记来侦听任何更改:

google.maps.event.addListener(marker, 'dragend', function(){
    //Do something because marker has been dragged somewhere...
});

最后,每次单击地图或拖动标记时,您都需要将生成的纬度和经度值保存在隐藏字段中,以便将位置保存在数据库中。

于 2013-08-01T11:48:46.287 回答