javascript - Google map + 街景问题 - 如何禁用 Photo Sphere

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

所以在我的网站上,我使用的是 Google map + 街景: https://developers.google.com/maps/documentation/javascript/examples/streetview-simple

此外,我使用的是具有自动完成功能的标准搜索框,问题是当我输入某些位置时,没有街景,只有 Photo Sphere 图像,没有像标准街景中那样四处移动的任何控件...

我真的不想要 Photo Sphere,因为我希望我的用户可以在街景 View 中自由移动,现在他们有时会“被困”在一张 Photo Sphere 图片中……有没有一种方法可以强制街景 View 而无需全景照片?

最佳答案

我不确定如何关闭 PhotoSpheres,但我确实找到了一个可能对您有用的解决方法。我注意到 https://developers.google.com/maps/documentation/javascript/reference#StreetViewSource在搜索位置时,如果将源设置为室外,则不会返回 PhotoSpheres。

因此,如果您为位置变化添加监听器,然后在没有 PhotoSphere 的情况下搜索位置,我认为您应该能够阻止它们出现。

这里是一个修改过的谷歌地图示例来说明:

<!DOCTYPE html>
<html>
<head>
	<title>Place Autocomplete without PhotoSpheres</title>
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
	<meta charset="utf-8">
	<style>
		html, body {
			height: 100%;
			margin: 0;
			padding: 0;
		}
		#map, #pano {
			height: 100%;
			width: 50%;
			float:left;
		}
		.controls {
			margin-top: 10px;
			border: 1px solid transparent;
			border-radius: 2px 0 0 2px;
			box-sizing: border-box;
			-moz-box-sizing: border-box;
			height: 32px;
			outline: none;
			box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
		}

		#pac-input {
			background-color: #fff;
			font-family: Roboto;
			font-size: 15px;
			font-weight: 300;
			margin-left: 12px;
			padding: 0 11px 0 13px;
			text-overflow: ellipsis;
			width: 300px;
		}

		#pac-input:focus {
			border-color: #4d90fe;
		}

		.pac-container {
			font-family: Roboto;
		}

		#type-selector {
			color: #fff;
			background-color: #4d90fe;
			padding: 5px 11px 0px 11px;
		}

		#type-selector label {
			font-family: Roboto;
			font-size: 13px;
			font-weight: 300;
		}

	</style>
</head>
<body>
<input id="pac-input" class="controls" type="text"
       placeholder="Enter a location">
<div id="map"></div>
<div id="pano"></div>
<script>

	var map;
	var panorama;
	var streetViewService;
	var DEFAULT_PROXIMITY = 50;
	var MAX_PROXIMITY = 10000;

	function initMap() {
		map = new google.maps.Map(document.getElementById('map'), {
			center: {lat: -33.8688, lng: 151.2195},
			zoom: 13
		});
		var input = /** @type {!HTMLInputElement} */(
			document.getElementById('pac-input'));

		map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

		var autocomplete = new google.maps.places.Autocomplete(input);
		//autocomplete.bindTo('bounds', map);

		streetViewService = new google.maps.StreetViewService();
		panorama = new google.maps.StreetViewPanorama(
			document.getElementById('pano'), {
				position:  {lat: -33.8688, lng: 151.2195},
				pov: {
					heading: 34,
					pitch: 10
				}
			});
		map.setStreetView(panorama);

		autocomplete.addListener('place_changed', function() {
			var place = autocomplete.getPlace();
			if (!place.geometry) {
				window.alert("Autocomplete's returned place contains no geometry");
				return;
			}
			// If the place has a geometry, then present it on a map.
			if (place.geometry.location) {
				findClosestStreetView(place.geometry.location, DEFAULT_PROXIMITY);
			}
		});

		function findClosestStreetView(point, proximity) {
			streetViewService.getPanorama({location: point, radius: proximity, source: google.maps.StreetViewSource.OUTDOOR}, function processSVData(data, status) {
				if (status == google.maps.StreetViewStatus.OK) {
					map.panTo(data.location.latLng);
					panorama.setPano(data.location.pano);
				} else {
					if (proximity < MAX_PROXIMITY) {
						findClosestStreetView(point, proximity + 50);
					} else {
						// NO PANARAMA FOUND - do something else here...
						panorama.setVisible(false);
					}
				}
			});
		}

	}

</script>
<script src="https://maps.googleapis.com/maps/api/js?&libraries=places&callback=initMap" async defer></script>
</body>
</html>

关于javascript - Google map + 街景问题 - 如何禁用 Photo Sphere,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33563551/

相关文章:

android - 如何获取标记文本的点击事件

javascript - D3 折线图 : Setting Y. 以特定数字为中心的域值

javascript - 如何将 TypeScript 项目编译为单个 JS 文件,以便可以在浏览器中使用

java - Spring 启动和 tomcat

java - 跟踪号码的 Fedex 校验和算法?

Android 谷歌地图多边形点击事件

ios - 用于启动 Google map 的 Url 架构在 iOS 9 中抛出警告并且也无法正常工作

javascript - 从网页中的 JavaScript 函数访问 PHP 变量

javascript - HTML 切换菜单消失

java - 使用 Maven ScmProvider.list 方法时,短划线转换为连字符