多个谷歌位置请求功能(药房定位器)
Multiple google places request function (dispensary locator)
我有一个很小的谷歌地图javascript v3商店定位器,我正在使用它来显示大约5家商店的位置-我的问题是,是否有比拥有5个单独的窗口更聪明的方法来创建位置请求和信息窗口:
//places request #1
//place ID for first location
servicedcd.getDetails({placeId: 'ChIJy_YmBMEMIocRZF8r5wPFMYU'},
function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: 'img/pillicon.jpg'});
targetLoc = place.geometry.location;
google.maps.event.addListener(marker, 'click', function() {
dcdLoc = place.geometry.location;
infowindow.setContent('<div><strong class="dmap-title">' + place.name + '</strong></div><br>' +
'<div class="dmap-address">' + place.formatted_address + '</div>' +
'<div class="dmap-phone">' + place.formatted_phone_number + '</div>' +
'<div class="dmap-rating">' + place.rating + '</div>' +
'<button onClick="getDirNew(dcdLoc);"><h3>Directions to ' + place.name + '</h3></button>' +
'<a href="' + place.website + '"><h4>Website</h4></a>' +
+ 'Place ID: ' + place.place_id + '<br>' +
place.formatted_address + '</div>');
infowindow.open(map, this);
console.log('new dcd geo loc: ' + place.geometry.location);
});
}
});
//places request #2
//place ID for second location
servicedcd.getDetails({placeId: 'ChIJy_YmBMEMIocRZF8r5wPFMYU'},
function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: 'img/pillicon.jpg'});
targetLoc = place.geometry.location;
google.maps.event.addListener(marker, 'click', function() {
dcdLoc = place.geometry.location;
infowindow.setContent('<div><strong class="dmap-title">' + place.name + '</strong></div><br>' +
'<div class="dmap-address">' + place.formatted_address + '</div>' +
'<div class="dmap-phone">' + place.formatted_phone_number + '</div>' +
'<div class="dmap-rating">' + place.rating + '</div>' +
'<button onClick="getDirNew(dcdLoc);"><h3>Directions to ' + place.name + '</h3></button>' +
'<a href="' + place.website + '"><h4>Website</h4></a>' +
+ 'Place ID: ' + place.place_id + '<br>' +
place.formatted_address + '</div>');
infowindow.open(map, this);
console.log('new dcd geo loc: ' + place.geometry.location);
});
}
});
等等。我知道这很糟糕。我已经准备好在的AWS上测试了
http://52.10.135.217/
如果有人想看看并给我建议
非常感谢
您当前有一个匿名函数,将其作为第二个参数传递给servicedcd.getDetails
:
然而,你可以做的是将其创建为一个单独的函数,然后只通过名称传递,例如
function addMarker(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: 'img/pillicon.jpg'});
targetLoc = place.geometry.location;
google.maps.event.addListener(marker, 'click', function() {
dcdLoc = place.geometry.location;
infowindow.setContent('<div><strong class="dmap-title">' + place.name + '</strong></div><br>' +
'<div class="dmap-address">' + place.formatted_address + '</div>' +
'<div class="dmap-phone">' + place.formatted_phone_number + '</div>' +
'<div class="dmap-rating">' + place.rating + '</div>' +
'<button onClick="getDirNew(dcdLoc);"><h3>Directions to ' + place.name + '</h3></button>' +
'<a href="' + place.website + '"><h4>Website</h4></a>' +
+ 'Place ID: ' + place.place_id + '<br>' +
place.formatted_address + '</div>');
infowindow.open(map, this);
console.log('new dcd geo loc: ' + place.geometry.location);
});
}
}
servicedcd.getDetails({placeId: 'ChIJy_YmBMEMIocRZF8r5wPFMYU'}, addMarker);
然后,如果唯一不同的是地方ID,你可以把它们放进一个循环的数组中:
placeIDs = [
'ChIJy_YmBMEMIocRZF8r5wPFMYU',
'ID 2',
'ID 3'
];
for (var i = 0; i < placeIDs.length; i++) {
servicedcd.getDetails({placeId: placeIDs[i]}, addMarker);
}
相关文章:
- 组合的量角器定位器
- 使用角度间隔计时器功能
- 找不到定位器Javascript
- jquery ajax调用后,表分类器功能不起作用
- 我的数字检查器功能不起作用,为什么
- 在商店定位器谷歌地图api中的商店面板中添加新字段
- 流星是否提供时间选择器功能
- 倒计时计时器功能和带有html标签的php
- 确定用于捕获对象的正确定位器
- 谷歌地图商店定位器只返回一个结果
- 难以理解的工作装饰器功能
- j查询验证器功能覆盖问题
- 如何在保留 JavaScript 调试器功能的同时进行优雅的异常处理
- j查询存储定位器标记
- 谷歌地图商店定位器将硬编码初始化修改为动态
- JQuery 日期选择器功能不起作用
- PhoneGap地理定位器使用WiFi而不是GPS
- 谷歌地图商店定位器 点击跳转到页面顶部
- 多个谷歌位置请求功能(药房定位器)
- Selenium-IDE:使用带有UI-Element映射功能的qooxdoo用户扩展定位器