我正在Rails中开发Web应用程序。当我打开一个标记时,一个模式将弹出一个框中的街景。我可以打开一个或两个标记,但在此之后,我收到WebGL遇到的一个错误。我试图在网上寻找资源,但没有任何意义。请参阅下面的图片了解更多信息。任何帮助将不胜感激。重复使用模式中的Google Maps街景
这里是什么我的控制台日志看起来像一个形象:
这里是我的webapp我的JavaScript代码。
<script type="text/javascript">
var handler = Gmaps.build('Google', {
markers: {
clusterer: {
gridSize: 60,
maxZoom: 20,
styles: [
{
textSize: 10,
textColor: '#ff0000',
url: 'assets/creative/m1.png',
height: 60,
width: 60,
},
{
textSize: 14,
textColor: '#ffff00',
url: 'assets/creative/m2.png',
height: 60,
width: 60,
},
{
textSize: 18,
textColor: '#0000ff',
url: 'assets/creative/m3.png',
width: 60,
height: 60,
},
],
},
},
});
var handler2 = Gmaps.build('Google');
var current;
function initialize() {
handler.buildMap({ internal: {id: 'map'} }, function() {
markers_json = <%= raw @hash.to_json %>;
markers = _.map(markers_json, function (marker_json) {
marker = handler.addMarker(marker_json);
handler.fitMapToBounds();
_.extend(marker, marker_json);
return marker;
});
getLocation();
markers.map(function (elem, index) {
google.maps.event.addListener(elem.getServiceObject(), 'click', function (evt) {
var id = elem.id,
number = elem.number,
name = elem.name,
zipcode = elem.zipcode,
tabid = elem.tabid,
latitude = elem.latitude,
longitude = elem.longitude;
$('.name').html('<h3 class=\'panel-title\'><i class=\'fa fa-id-card\'></i>' + number + '</h3>');
$('.paneltb').html('<thead><tr><th>Panel</th><th>Location</th><th>Tab ID</th><th>Zip Code</th><th>Latitude</th><th>Longitude</th></tr></thead><tbody><tr><td>' + number + '</td><td>' + name + '</td><td>' + tabid + '</td><td>' + zipcode + '</td><td>' + latitude + '</td><td>' + longitude + '</td></tr></tbody>');
pos = new google.maps.LatLng(latitude, longitude);
var div = document.getElementById('map2');
var sv = new google.maps.StreetViewPanorama(div);
sv.setPosition(pos);
sv.setVisible(true);
// find the heading by looking from the google car pos to the venue pos
var service = new google.maps.StreetViewService();
service.getPanoramaByLocation(pos, 50, function (result, status) {
if (status == google.maps.StreetViewStatus.OK)
{
carPos = result.location.latLng;
heading = google.maps.geometry.spherical.computeHeading(carPos, pos);
sv.setPov({ heading: heading, pitch: 0, zoom: 1 });
}
});
$('#myModal').modal('show');
current = elem;
});
});
});
// Create the search box and link it to the UI element.
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayOnMap);
} else {
navigator.geolocation.getCurrentPosition(displayOnMapError);
}
}
function displayOnMap(position) {
marker2 = handler.addMarker({
lat: position.coords.latitude,
lng: position.coords.longitude,
picture: {
url: "<%= asset_path 'creative/1499326997_Untitled-2-01.png' %>",
width: 48,
height: 48,
},
infowindow: 'You are Here!',
});
handler.map.centerOn(marker2);
handler.getMap().setZoom(10);
}
function displayOnMapError(position) {
marker2 = handler.addMarker({
lat: 34.0522,
lng: -118.2437,
picture: {
url: "<%= asset_path 'creative/1499326997_Untitled-2-01.png' %>",
width: 48,
height: 48,
},
});
handler.map.centerOn(marker2);
handler.getMap().setZoom(10);
}
initialize();
</script>
这是最初的代码,让模态弹出与我需要的信息。
$(".name").html("<h3 class='panel-title'><i class='fa fa-id-card'></i>"+number+"</h3>");
$(".paneltb").html("<thead><tr><th>Panel</th><th>Location</th><th>Tab ID</th><th>Zip Code</th><th>Latitude</th><th>Longitude</th></tr></thead><tbody><tr><td>"+number+"</td><td>"+ name + "</td><td>"+tabid+"</td><td>"+zipcode+"</td><td>"+latitude+"</td><td>"+longitude+"</td></tr></tbody>");
$('#myModal').modal('show');
current = elem;
为了增加模态中添加以下代码内的街道视图:
pos = new google.maps.LatLng(latitude, longitude);
var div = document.getElementById('map2');
var sv = new google.maps.StreetViewPanorama(div);
sv.setPosition(pos);
sv.setVisible(true);
// find the heading by looking from the google car pos to the venue pos
var service = new google.maps.StreetViewService();
service.getPanoramaByLocation(pos, 50, function (result, status) {
if (status == google.maps.StreetViewStatus.OK) {
carPos = result.location.latLng;
heading = google.maps.geometry.spherical.computeHeading(carPos, pos);
sv.setPov({ heading: heading, pitch: 0, zoom: 1 });
}
});
为了通过目前的经纬度,并长成街景,我不得不把它与模态被调用的功能相同。
UPDATE
我想不出如何设置var sv = new google.maps.StreetViewPanorama(div);
因此它被设置一次和模态的每个实例被称为,而不是试图启动和重新启动一个新的实例重复使用相同的地图。
更新2
我无法弄清楚如何开始初始化此部分:
var sv = new google.maps.StreetViewPanorama(div);
所以,当我打开一个模式就不会呈现一个新的地图,它只是重复使用相同的地图。我倾向于写另一个功能,但我需要一些指导。
更新3
别的东西,我注意到的是,当我点击一个标志,它会显示在街景中的模态尽显其方。当我点击一个又一个,有时它不会显示在所有,但在大多数情况下,它显示却在角落里非常小的,因为这形象:
我也注意到,代码widget-scene-canvas
类来自谷歌地图不断改变自己从我原本是通过自己的身份证造型,当我点击超过第一张地图在不同的时间。
这是它的代码中的街道视图右侧下方的模式。 –
$('#myModal')。modal('show')current = elem; }); –
我不知道你是否使用教程,但无论如何,我的建议是你移动与打开和关闭该脚本的模式有关的所有内容,然后每次将“lnglat”传递给streetview实例用户打开它,无论你想要什么事件绑定。而且,从我所看到的,你已经这样做了,所以它不应该太困难。 – Crashtor