当我单击按钮时,将显示路线。而且我还希望将路线的地址显示在div上。
路线确定,但地址尚未解决。地址无法自动显示。
我认为问题出在地理编码器上。
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
$(document).ready(function(){
$('#first').click(function(){
initMap(0,2);
});
});
$(document).ready(function(){
$('#second').click(function(){
initMap(0,4);
});
});
</script>
<script>
var map;
var directionsDisplay;
var directionsService;
var locations = [
[25.1348161076504,121.67990112304688],
[25.102487193702274,121.357177734375],
[25.019138692693637,121.18826293945312],
[24.84135795428775, 121.07004752388548],
[24.706692402319927,120.99314322701048],
[24.640730624546876,120.93036230807502],
[24.548110180862288,120.90421412458]
];
function initMap(a,b) {
directionsDisplay=new google.maps.DirectionsRenderer();
directionsService=new google.maps.DirectionsService();
var map = new google.maps.Map(document.getElementById('map'), {
center:{lat:24,lng:121},
zoom: 8
});
directionsDisplay.setMap(map);
var infowindow=new google.maps.InfoWindow();
var marker,i;
var request={
travelMode:'DRIVING'
};
var labels='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for(i=a;i<b;i++){
marker=new google.maps.Marker({
position:{
lat : parseFloat(locations[i][0]),
lng : parseFloat(locations[i][1])
},label:labels[i%labels.length]
,map:map
});
google.maps.event.addListener(marker,'click',(function(marker,i){
})(marker,i));
if (i == a) {
request.origin = marker.getPosition();
marker.setVisible(false);
} else if (i == b-1) {
request.destination = marker.getPosition();
marker.setVisible(false);
} else {
if (!request.waypoints) request.waypoints = [];
request.waypoints.push({
location: marker.getPosition(),
stopover: true,
});marker.setVisible(false);
}
}
directionsService.route(request, function(result,status){
if(status==='OK'){
directionsDisplay.setDirections(result);
}
});
for(var y = a; y < b; y++){
var geocoder = new google.maps.Geocoder();
var coord = new google.maps.LatLng(locations[y][0],locations[y][1]);
geocoder.geocode({'latLng':coord},function(results, status){
if(status==='OK'){
if(results){
document.getElementById("address").innerHTML+=results[0].formatted_address;
}
}
});
}
}
我试图将地址放入数组中,只是可以像
那样写出数组的值var array1=[];
var array2=[];
array1.push(results[0].formatted_address);
array2=array1[0]+array1[1]+array[2]+...;
document.getElementById("address").innerHTML=array2;