【发布时间】:2014-05-27 16:08:09
【问题描述】:
如果有人帮助我解决我一直存在的问题,我会很高兴! 我正在尝试使用 Laravel、AngularJS 和 Google Maps API 创建一个旅行计划器。 基本上我想做的是:我有三个文本输入字段,我想获取文本值并在谷歌地图上的那个地方添加一个标记(这里没有显示地图!),如图所示!
我在 index.php 中有这个,我应该添加其他内容吗?
<div id="map-container" ng-controller="mapCtrl" class="map-container col-xs-12 col-sm-7 col-md-8 nopadding">
<google-map draggable="true" center="map.center" zoom="map.zoom" options="map.options"></google-map>
</div>
<form ng-submit="submitTrip()" ng-Enter=""> <!-- ng-submit will disable the default form action and use our function -->
<!-- START CITY -->
<div class="form-group col-xs-12">
<input type="text" class="form-control input-lg" id ="start_city" name="start_city" ng-model="tripData.start_city" googleplace autocomplete="off" placeholder="Travel from" required>
</div>
<!-- VIA CITY -->
<div class="form-group col-xs-12">
<input type="text" class="form-control input-lg" name="via_city" ng-model="tripData.via_city" googleplace autocomplete="off" placeholder="Travel via" required>
</div>
<!-- END CITY -->
<div class="form-group col-xs-12">
<input type="text" class="form-control input-lg" name="end_city" ng-model="tripData.end_city" googleplace autocomplete="off" placeholder="Travel to" required>
</div>
</form>
这是我正在使用的角度模块
angular.module('tripCtrl', ['ui.router', 'google-maps'])
.directive('googleplace', function() {
return {
require: 'ngModel',
link: function($scope, element) {
var autocomplete = new google.maps.places.Autocomplete(element[0]);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
var map,
formatted_address = place.formatted_address,
mapDiv = document.getElementById('map-container'),
geocoder = new google.maps.Geocoder();
latLng = new google.maps.LatLng(place.geometry.location.lat(),place.geometry.location.lng());
// Get LatLng information by name
geocoder.geocode({
address: formatted_address,
location: latLng
}, function(results){
map = new google.maps.Map(mapDiv, {
// Center map (but check status of geocoder)
center: results[0].geometry.location,
zoom: 5,
mapTypeId: google.maps.MapTypeId.TRANSIT
});
$scope.addMarker(place); /*Want to add a marker every time we type in something i the text-inputs and save it there*/
});
}),
$scope.addMarker = function (item) {
angular.extend($scope, {
markers: {
store: {
lat: item.geometry.location.lat(),
lng: item.geometry.location.lng(),
}
}
});
};
}
};
});
从代码中可以看出,我想创建一个名为 addMarker 的函数,并在每次在输入字段中输入内容时调用此函数,并在纬度、经度位置放置一个标记并将该标记保存在那里直到页面关闭.
所以问题是如何在文本字段(即挪威奥斯陆)中输入文本后放置标记,然后在输入第二个输入“Travel via”时放置另一个标记,这两个标记保留在地图上并且很快 ?
所以如果有人有解决方案,请分享你的知识,我将不胜感激:)
【问题讨论】:
-
我在阅读您的文字和代码时的第一个想法是:'是的,太棒了!这样做'我的第二个是'但是你的问题是什么?'
-
感谢您的注解!我刚改了,这个问题可以理解吗?
-
it is =) 我是否理解正确的问题:添加第二个标记时,第一个会消失?看起来您每次调用 $scope.addMarker 函数时都在覆盖 $scope.markers 。我正在使用传单地图,所以不能 100% 确定这是否适合你。尝试扩展 $scope.markers: angular.extend($scope.markers, {lat:...,lng:...})
标签: javascript angularjs google-maps laravel