Подтвердить что ты не робот

Рабочий пример функции поиска angular -google-maps

Есть ли у кого-нибудь пример рабочего окна поиска, такого как команда angular -google-maps-team отображается в поле поиска на этом сайте: https://angular-ui.github.io/angular-google-maps/#!/api

Если вы пишете что-то, то обязательно найдете его в раскрывающемся списке, но когда вы нажимаете клавишу ввода, карта не отвечает. - Как вы можете переместить карту в нужное место при нажатии кнопки?

4b9b3361

Ответ 1

HTML:

<ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="true">
    <ui-gmap-search-box template="searchbox.template" events="searchbox.events" position="BOTTOM_RIGHT"></ui-gmap-search-box>
    <ui-gmap-marker coords="marker.coords" options="marker.options" events="marker.events" idkey="marker.id">
</ui-gmap-google-map>
Контроллер

js:

$scope.map = {
    "center": {
        "latitude": 52.47491894326404,
        "longitude": -1.8684210293371217
    },
    "zoom": 15
}; //TODO:  set location based on users current gps location 
$scope.marker = {
    id: 0,
    coords: {
        latitude: 52.47491894326404,
        longitude: -1.8684210293371217
    },
    options: { draggable: true },
    events: {
        dragend: function (marker, eventName, args) {

            $scope.marker.options = {
                draggable: true,
                labelContent: "lat: " + $scope.marker.coords.latitude + ' ' + 'lon: ' + $scope.marker.coords.longitude,
                labelAnchor: "100 0",
                labelClass: "marker-labels"
            };
        }
    }
};
var events = {
    places_changed: function (searchBox) {
        var place = searchBox.getPlaces();
        if (!place || place == 'undefined' || place.length == 0) {
            console.log('no place data :(');
            return;
        }

        $scope.map = {
            "center": {
                "latitude": place[0].geometry.location.lat(),
                "longitude": place[0].geometry.location.lng()
            },
            "zoom": 18
        };
        $scope.marker = {
            id: 0,
            coords: {
                latitude: place[0].geometry.location.lat(),
                longitude: place[0].geometry.location.lng()
            }
        };
    }
};
$scope.searchbox = { template: 'searchbox.tpl.html', events: events };

Ответ 3

// the following controls the map in your Controller
$scope.map = { control: {}, center: { latitude: 37.70, longitude: -122.344 }, zoom: 9, refresh: {}};

function placeToMarker(searchBox, id) {

  var place = searchBox.getPlaces();
  if (!place || place == 'undefined' || place.length == 0) {
    return;
  }

  var marker = {
    id: id,
    place_id: place[0].place_id,
    name: place[0].name,
    address: place[0].formatted_address,
    latitude: place[0].geometry.location.lat(),
    longitude: place[0].geometry.location.lng(),
    latlng: place[0].geometry.location.lat() + ',' + place[0].geometry.location.lng()
  };
// push your markers into the $scope.map.markers array
if (!$scope.map.markers) {
    $scope.map.markers = [];
  }

// THIS IS THE KEY TO RECENTER/REFRESH THE MAP, to your question
$scope.map.control.refresh({latitude: marker.latitude, longitude: marker.longitude});

// the following defines the SearchBox on your Controller; events call placeToMarker function above
var searchBoxEvents = {
  places_changed: function (searchBox) {
    placeToMarker(searchBox, id);
  }
};

// this is defined on the Controller, as well. This specifies which template searchBoxEvents should match to; note the parentdiv
  $scope.searchBox = { template:'searchBox.template.html', events:searchBoxEvents, parentdiv: 'searchBoxParent'};

// in your HTML, declare where you want the searchBox. parentdiv: 'searchBoxParent' above looks for the id="searchBoxParent" in HTML
<div class="col-xs-12 col-md-12" id="searchBoxParent">
  <script type="text/ng-template" id="searchBox.template.html">
    <input type="text" ng-model="address" placeholder="Search Address" required />
  </script>
</div>

//Lastly, in HTML, make sure you wrap ui-gmap-search-box & ui-gmap-markers in ui-gmap-google-map
<ui-gmap-google-map id="map-canvas" center="map.center" zoom="map.zoom" draggable="true" options="options" control="map.control">
    <ui-gmap-search-box template="searchBox.template" events="searchBox.events" parentdiv="searchBox.parentdiv"></ui-gmap-search-box>
    <ui-gmap-markers idkey="map.idkey" models="map.markers" coords="'self'" icon="'icon'" click="'onClicked'" fit="true"></ui-gmap-markers>
  </ui-gmap-google-map>

Ответ 4

Ответ Gavin верен, а еще несколько подробностей о "searchbox.tpl.html его примера. Он должен располагаться вне директивы:

<body>
    <div id="searchBoxParent"></div>
    <div id="map_canvas" ng-controller="mainCtrl">
    <script type="text/ng-template" id="searchbox.tpl.html">
        <input type="text" placeholder="Search Box">
    </script>
    <ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="true" options="options">
        <ui-gmap-search-box template="searchbox.template" events="searchbox.events" parentdiv="searchbox.parentdiv"></ui-gmap-search-box>
    </ui-gmap-google-map>
</div>
<!--example-->
</body>

Рабочий plunkr: http://embed.plnkr.co/1rpXQhcZqwJ7rv0tyK9P/ (по какой-то причине plunkr работал только в хром для меня, но не в firefox)

Я не мог прокомментировать ответ Гэвина из-за отсутствия репликации, поэтому я добавляю эту информацию в качестве дополнительного ответа.