javascript - 如何通过自动完成在 Angular 谷歌地图上放置标记?

标签 javascript angularjs google-maps google-maps-api-3

我想通过自动完成搜索在谷歌 Angular map 上放置标记,我正在以纬度和经度的形式获取位置,但标记没有添加,请检查......

Controller 代码为

$timeout(function() {

          var input = document.getElementById('pac-input');
          var searchBox = new google.maps.places.SearchBox(input);

          google.maps.event.addListener(searchBox, 'places_changed', function() {
             var marker= [] ;
             var places = searchBox.getPlaces();

             if (places.length == 0) {
               return;
             }

            var loc_data = {            

              coords: {
                  latitude: places[0].geometry.location.lat(),
                  longitude: places[0].geometry.location.lng()
              }
            };
            marker.push(loc_data);
            $scope.markers=marker;              
          });

HTML代码是

    <ui-gmap-markers models="markers" idkey="markers.id" coords="'coords'" >

    </ui-gmap-markers>

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

    <input id="pac-input" class="controls" type="text" placeholder="Search Box">
</div>

最佳答案

 <div class="form-group">
                                <label for="pacinput" class="control-label col-md-4">Search Location</label>
                                <div class="col-md-8">
                                    <input id="pacinput" class="form-control" type="text" placeholder="Search Location">
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-offset-4 col-md-8">
                                    <asp:HiddenField runat="server" ID="address" />
                                    <asp:HiddenField ID="txtLat" runat="server" />
                                    <asp:HiddenField ID="txtLng" runat="server" />
                                    <div id="map" style="width: 100%; height: 380px;">
                                    </div>
                                </div>
                            </div>

//此示例使用 Google 地点自动完成功能向 map 添加搜索框 //特征。人们可以输入地理搜索。搜索框将返回一个 //选择包含地点和预测搜索词混合的列表。

    // This example requires the Places library. Include the libraries=places
    // parameter when you first load the API. For example:
    // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

    function initAutocomplete() {
        var map = new google.maps.Map(document.getElementById('map'), {
            center: { lat: 15.362813, lng: 75.126129 },
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        // Create the search box and link it to the UI element.
        var input = document.getElementById('pacinput');
        var searchBox = new google.maps.places.SearchBox(input);

        // Bias the SearchBox results towards current map's viewport.
        map.addListener('bounds_changed', function () {
            searchBox.setBounds(map.getBounds());
        });

        var markers = [];
        // Listen for the event fired when the user selects a prediction and retrieve
        // more details for that place.
        searchBox.addListener('places_changed', function () {
            var places = searchBox.getPlaces();

            if (places.length == 0) {
                return;
            }

            // Clear out the old markers.
            markers.forEach(function (marker) {
                marker.setMap(null);
            });
            markers = [];

            // For each place, get the icon, name and location.
            var bounds = new google.maps.LatLngBounds();
            places.forEach(function (place) {
                var 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(25, 25)
                };

                // Create a marker for each place.
                markers.push(new google.maps.Marker({
                    map: map,
                    icon: icon,
                    title: place.name,
                    position: place.geometry.location
                }));

                if (place.geometry.viewport) {
                    // Only geocodes have viewport.
                    bounds.union(place.geometry.viewport);
                } else {
                    bounds.extend(place.geometry.location);
                }
            });
            map.fitBounds(bounds);
        });
    }

</script>

关于javascript - 如何通过自动完成在 Angular 谷歌地图上放置标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33038215/

相关文章:

angularjs - 为什么 $state.transitionTo 或 $state.go 不显示新的 HTML 部分?

json - 如何通过 angularjs $resource 访问 json 中的嵌套对象

javascript - 使用 jquery 获取排序后的第一个子 <div> 类名

javascript - 使用 StupidTable JS 和日期列进行排序

javascript - 将 node.js 缓冲区转换为 ref-struct 实例

javascript - AngularJs 总是将最后一个类应用于 body 标签

javascript - 如何在 google 闭包库上修复 sha256 hmac 的加密?

java - map Activity 在 getFragmentById 上崩溃

jquery - 如何在 Bootstrap jQuery Tab 中重新加载 Google map

google-maps - 如何单击 Xamarin UITest 的谷歌地图上的标记