Javascript 如何让谷歌地图API停留在一个点上
我有一个谷歌地图在我的网站上工作,但我希望它只集中在纽约。我不希望地图功能在纽约以外的地方工作。我该怎么做?我已经访问了用户的位置,然后将他们定向到地图,但是,我是否应该删除该功能,让地图只关注某些坐标?谢谢你的帮助Javascript 如何让谷歌地图API停留在一个点上,javascript,google-maps,Javascript,Google Maps,我有一个谷歌地图在我的网站上工作,但我希望它只集中在纽约。我不希望地图功能在纽约以外的地方工作。我该怎么做?我已经访问了用户的位置,然后将他们定向到地图,但是,我是否应该删除该功能,让地图只关注某些坐标?谢谢你的帮助 var map; var service; function initialise(location) { console.log("location:" + location); var currentLocation = new google.maps.Lat
var map;
var service;
function initialise(location) {
console.log("location:" + location);
var currentLocation = new google.maps.LatLng(location.coords.latitude, location.coords.longitude);
var mapOption = {
center : currentLocation,
zoom : 14,
mapTypeId : google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOption);
var marker = new google.maps.Marker({
position : currentLocation,
map : map,
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png');
service = new google.maps.places.PlacesService(map);
google.maps.event.addListenerOnce(map, 'bounds_changed', performSearch);
function handleSearchResults(results, status) {
console.log(results)
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map : map,
position : place.geometry.location
});
....
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
var latitude = this.position.lat();
var longitude = this.position.lng();
console.log(this.position);
});
}
google.maps.event.addListenerOnce(map, 'bounds_changed', function() {
var input = $("#search").val();
var query = (input != '' )? input : "starbucks";
performSearch(query);
});
function performSearch(q){
var request ={
bounds: map.getBounds(),
query:String(q)
};
service.textSearch(request, callback);
}
}
function initializer() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 13,
});
if(navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
map.setCenter(initialLocation);
var marker = new google.maps.Marker({
position : initialLocation,
map : map,
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png');
}, function() {
handleNoGeolocation(browserSupportFlag);
});
}
}
google.maps.event.addDomListener(window, 'load', initializer);
$(".getSearch").click(function () {
navigator.geolocation.getCurrentPosition(initialise);
});
});
var映射;
var服务;
函数初始化(位置){
控制台日志(“位置:+位置);
var currentLocation=new google.maps.LatLng(location.coords.latitude,location.coords.longitude);
变量映射选项={
中心:当前位置,
缩放:14,
mapTypeId:google.maps.mapTypeId.ROADMAP,
};
map=new google.maps.map(document.getElementById(“地图画布”),mapOption);
var marker=new google.maps.marker({
位置:当前位置,
地图:地图,
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png');
服务=新的google.maps.places.PlacesService(地图);
google.maps.event.addListenerOnce(映射'bounds_changed',performSearch);
函数handleSearchResults(结果、状态){
console.log(结果)
}
函数回调(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
对于(var i=0;i
以下是您可以做的:
创建一个边界框,将贴图范围限制为仅限于纽约市区域
实现这一目标有多种方法:
为了简单使用,当发生事件bounds\u changed
时,您可以绑定一个处理程序将映射边界(panToBounds
)更改回默认边界