我的页面中有以下代码(基于谷歌示例的时刻)。 然而,我遇到了障碍,我似乎无法让地方工作(标记不会显示),这是我的代码。 。 。它应该显示距离地理位置10公里范围内的所有咖啡馆,餐馆和dars,它还会根据用户当前位置向澳大利亚达尔文发送指示。
这是代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Maps JavaScript API v3 Example: Directions Complex</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map_canvas {
height: 650px;
}
#directions-panel {
height: 100%;
float: right;
width: 390px;
overflow: auto;
}
#map-canvas {
margin-right: 400px;
}
#control {
background: #fff;
padding: 5px;
font-size: 14px;
font-family: Arial;
border: 1px solid #ccc;
box-shadow: 0 2px 2px rgba(33, 33, 33, 0.4);
display: none;
}
@media print {
#map-canvas {
height: 500px;
margin: 0;
}
#directions-panel {
float: none;
width: auto;
}
}
.gmnoprint {display:none;}
</style>
<script src="https://maps.googleapis.com/maps/api/js?sensor=true&libraries=places"></script>
<script>
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var a;
var b;
var latandlon;
function initialize()
{
navigator.geolocation.getCurrentPosition(create_a_var);
}
function create_a_var(position)
{
a = position.coords.latitude;
b = position.coords.longitude;
latandlon = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
loadmapscript();
}
function loadmapscript() {
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(a, b),
disableDefaultUI: true
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directions-panel'));
var control = document.getElementById('control');
control.style.display = 'block';
map.controls[google.maps.ControlPosition.TOP].push(control);
var request = {
location: latandlon,
radius: 10000,
types: ['bar', 'restaurant', 'cafe']
};
}
function calcRoute() {
var start = latandlon;
var end = "Darwin, NT, Australia";
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
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() {
alert(place.name);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="control">
<strong>Start:</strong>
<button id="start" onclick="calcRoute();">Click Me Please :)</button>
</div>
<div id="directions-panel"></div>
<div id="map_canvas"></div>
</body>
</html>
var directionDisplay; var directionsService = new google.maps.DirectionsService(); var a; var b; var latandlon; var map; 函数initialize() { navigator.geolocation.getCurrentPosition(create_a_var); }
function create_a_var(position) { a = position.coords.latitude; b = position.coords.longitude; latandlon = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); loadmapscript(); } function loadmapscript() { directionsDisplay = new google.maps.DirectionsRenderer(); var mapOptions = { zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP, center: new google.maps.LatLng(a, b), disableDefaultUI: true }; map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); directionsDisplay.setMap(map); directionsDisplay.setPanel(document.getElementById('directions-panel')); var control = document.getElementById('control'); control.style.display = 'block'; map.controls[google.maps.ControlPosition.TOP].push(control); var request2 = { location: latandlon, radius: 10000, types: ['bar', 'restaurant', 'cafe'] }; } function calcRoute() { var start = latandlon; var end = "Darwin, NT, Australia"; var request = { origin: start, destination: end, travelMode: google.maps.DirectionsTravelMode.DRIVING }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } }); } 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() { alert(place.name); }); } google.maps.event.addDomListener(window, 'load', initialize);
答案 0 :(得分:2)
'map'是loadmapscript函数的局部变量。然后尝试在createMarker函数中引用它,该函数无权访问该变量。改为使地图成为全局变量。
在此处删除TravelMode行末尾的尾随逗号。这将在Internet Explorer上引发错误:
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
};
你有一个createMarker函数,但代码中没有任何东西调用该函数....因此没有标记。