将自定义标记添加到 google 街景
Adding a custom marker to google street view
请帮我在 google 街景中添加一个标记,但是当我把它添加进去时没有任何效果,我得到了一个
未捕获的 ReferenceError:地图未定义
这里是标记变量
var cafe = new google.maps.LatLng(34.935196, -107.539546);
这是设置标记的代码
var cafeMarker = new google.maps.Marker({
position: cafe,
map: map,
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
title: 'Cafe'
});
这是自定义 google 街景
<script src="https://maps.googleapis.com/maps/api/js? v=3.exp&signed_in=true"></script>
<script>
function initialize() {
var fenway = new google.maps.LatLng(34.938447, -107.535998);
var panoOptions = {
position: fenway,
addressControlOptions: {
position: google.maps.ControlPosition.BOTTOM_CENTER
},
linksControl: false,
panControl: false,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
enableCloseButton: false
};
var panorama = new google.maps.StreetViewPanorama(
document.getElementById('map-canvas'), panoOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
要在街景上看到标记,您必须将其添加到全景图中。 Uncaught ReferenceError: map is not defined
告诉你代码中没有定义 map
变量,它需要是 panorama
.
var cafeMarker = new google.maps.Marker({
position: cafe,
map: panorama, // your code doesn't have a 'map' variable
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
title: 'Cafe'
});
一旦我这样做了,你的标记就太远了,看不见。如果我将全景位置移近(并将其指向标记),我就会看到它。
function initialize() {
var fenway = new google.maps.LatLng(34.9355,-107.539254);
var panoOptions = {
position: fenway,
addressControlOptions: {
position: google.maps.ControlPosition.BOTTOM_CENTER
},
linksControl: false,
panControl: false,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
enableCloseButton: false
};
var panorama = new google.maps.StreetViewPanorama(
document.getElementById('map-canvas'), panoOptions);
var cafe = new google.maps.LatLng(34.935196, -107.539546);
var cafeMarker = new google.maps.Marker({
position: cafe,
map: panorama,
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
title: 'Cafe'
});
var heading = google.maps.geometry.spherical.computeHeading(panorama.getPosition(),cafe);
panorama.setPov({
heading: heading,
pitch: 0,
zoom: 1
});
}
代码片段:
function initialize() {
var fenway = new google.maps.LatLng(34.9355, -107.539254);
// var fenway = new google.maps.LatLng(34.938447, -107.535998);
var panoOptions = {
position: fenway,
addressControlOptions: {
position: google.maps.ControlPosition.BOTTOM_CENTER
},
linksControl: false,
panControl: false,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
enableCloseButton: false
};
var panorama = new google.maps.StreetViewPanorama(
document.getElementById('map-canvas'), panoOptions);
var cafe = new google.maps.LatLng(34.935196, -107.539546);
var cafeMarker = new google.maps.Marker({
position: cafe,
map: panorama,
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
title: 'Cafe'
});
var heading = google.maps.geometry.spherical.computeHeading(panorama.getPosition(), cafe);
panorama.setPov({
heading: heading,
pitch: 0,
zoom: 1
});
// Set up the map
var myOptions = {
zoom: 15,
center: cafe
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
myOptions);
var cafeMarkerMap = new google.maps.Marker({
position: cafe,
map: map,
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
title: 'Cafe'
});
var panoMarker = new google.maps.Marker({
position: panorama.getPosition(),
map: map,
icon: {
url: 'https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png',
size: new google.maps.Size(7, 7),
anchor: new google.maps.Point(3.5, 3.5)
},
title: 'Pano'
});
document.getElementById('info').innerHTML = google.maps.geometry.spherical.computeDistanceBetween(panorama.getPosition(), cafe).toFixed(2) + " meters";
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas,
#map_canvas {
height: 500px;
width: 500px;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas" style="border: 2px solid #3872ac;"></div>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
<div id="info"></div>
请帮我在 google 街景中添加一个标记,但是当我把它添加进去时没有任何效果,我得到了一个 未捕获的 ReferenceError:地图未定义
这里是标记变量
var cafe = new google.maps.LatLng(34.935196, -107.539546);
这是设置标记的代码
var cafeMarker = new google.maps.Marker({
position: cafe,
map: map,
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
title: 'Cafe'
});
这是自定义 google 街景
<script src="https://maps.googleapis.com/maps/api/js? v=3.exp&signed_in=true"></script>
<script>
function initialize() {
var fenway = new google.maps.LatLng(34.938447, -107.535998);
var panoOptions = {
position: fenway,
addressControlOptions: {
position: google.maps.ControlPosition.BOTTOM_CENTER
},
linksControl: false,
panControl: false,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
enableCloseButton: false
};
var panorama = new google.maps.StreetViewPanorama(
document.getElementById('map-canvas'), panoOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
要在街景上看到标记,您必须将其添加到全景图中。
Uncaught ReferenceError: map is not defined
告诉你代码中没有定义map
变量,它需要是panorama
.var cafeMarker = new google.maps.Marker({ position: cafe, map: panorama, // your code doesn't have a 'map' variable icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00', title: 'Cafe' });
一旦我这样做了,你的标记就太远了,看不见。如果我将全景位置移近(并将其指向标记),我就会看到它。
function initialize() { var fenway = new google.maps.LatLng(34.9355,-107.539254); var panoOptions = { position: fenway, addressControlOptions: { position: google.maps.ControlPosition.BOTTOM_CENTER }, linksControl: false, panControl: false, zoomControlOptions: { style: google.maps.ZoomControlStyle.SMALL }, enableCloseButton: false }; var panorama = new google.maps.StreetViewPanorama( document.getElementById('map-canvas'), panoOptions); var cafe = new google.maps.LatLng(34.935196, -107.539546); var cafeMarker = new google.maps.Marker({ position: cafe, map: panorama, icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00', title: 'Cafe' }); var heading = google.maps.geometry.spherical.computeHeading(panorama.getPosition(),cafe); panorama.setPov({ heading: heading, pitch: 0, zoom: 1 }); }
代码片段:
function initialize() {
var fenway = new google.maps.LatLng(34.9355, -107.539254);
// var fenway = new google.maps.LatLng(34.938447, -107.535998);
var panoOptions = {
position: fenway,
addressControlOptions: {
position: google.maps.ControlPosition.BOTTOM_CENTER
},
linksControl: false,
panControl: false,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
enableCloseButton: false
};
var panorama = new google.maps.StreetViewPanorama(
document.getElementById('map-canvas'), panoOptions);
var cafe = new google.maps.LatLng(34.935196, -107.539546);
var cafeMarker = new google.maps.Marker({
position: cafe,
map: panorama,
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
title: 'Cafe'
});
var heading = google.maps.geometry.spherical.computeHeading(panorama.getPosition(), cafe);
panorama.setPov({
heading: heading,
pitch: 0,
zoom: 1
});
// Set up the map
var myOptions = {
zoom: 15,
center: cafe
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
myOptions);
var cafeMarkerMap = new google.maps.Marker({
position: cafe,
map: map,
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
title: 'Cafe'
});
var panoMarker = new google.maps.Marker({
position: panorama.getPosition(),
map: map,
icon: {
url: 'https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png',
size: new google.maps.Size(7, 7),
anchor: new google.maps.Point(3.5, 3.5)
},
title: 'Pano'
});
document.getElementById('info').innerHTML = google.maps.geometry.spherical.computeDistanceBetween(panorama.getPosition(), cafe).toFixed(2) + " meters";
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas,
#map_canvas {
height: 500px;
width: 500px;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas" style="border: 2px solid #3872ac;"></div>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
<div id="info"></div>