所以我试图这样做:
- 输入地址
- 获取地址的纬度/经度
- 把它放在带有最终目的地(纬度/经度)的“方向”上
- 在我的两个 div 中获取路线
听起来很容易。该脚本返回“OK”状态。所以你应该认为它会起作用,但不是。
<div id="map_canvas" class="pic50"></div>
<div id="map_directions" class="text50"></div>
bring_me_back('map_canvas','map_directions','some address');
<script>
// bring me back
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
function bring_me_back(call_back_map,call_back_directions,address) {
var latlng = new google.maps.LatLng(51.764696,5.526042); // where you're at
directionsDisplay = new google.maps.DirectionsRenderer();
var myOptions = {
zoom: 14,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};
var map = new google.maps.Map(document.getElementById(call_back_map),myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById(call_back_directions));
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"My location"
});
// find the address
geocoder = new google.maps.Geocoder (map);
geocoder.geocode ( { 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var end = results [0].geometry.location; // where you need to go
map.setCenter(results [0].geometry.location);
marker.setPosition(results [0].geometry.location);
var start = "51.764696,5.526042"; // where you're at (random place)
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.WALKING
};
directionsService.route(request, function(response, status) {
alert("Directions was not successful for the following reason: " + status);
});
}
else {
$('#'+call_back).html("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
我知道我在做一些愚蠢的事情,因为我的错误控制台显示:目标:[我的网站]# 未找到
有趣的是,地图确实以带有标记的地理定位器的正确位置为中心。所以除了推回方向它做得很好(我认为)