【发布时间】:2016-07-05 15:42:58
【问题描述】:
我已经显示了来自 2 个标记的路线方向,标记 1 包含 lat 和 lng,标记 2 包含 lat2 和 lng2。从该标记中,地图将显示标记 1 和标记 2 的路线方向,我的问题是有许多标记,我想显示每对标记的所有路线方向,因此结果是许多路线方向将显示在地图上.帮我解决这个问题
检查我下面的代码
(我从一开始就添加了这个项目的完整代码)
// DIRECTION
function initMap() {
var ren, ser;
var marker, marker2;
var i, j;
var infowindow;
var directionsDisplay;
var pointA, pointB;
var locations = [
<?php
include('inc/config.php');
$sql_lokasi="select * from lokasi";
$result=mysql_query($sql_lokasi) or die(mysql_error());
while($data=mysql_fetch_object($result)){
?>
['<?=$data->nama;?>', <?=$data->lat;?>, <?=$data->lng;?>],
<?
}
?>
];
var locations2 = [
<?php
include('inc/config.php');
$sql_lokasi="select idlokasi,nama,lat,lng,lat2,lng2 from lokasi";
$result=mysql_query($sql_lokasi) or die(mysql_error());
while($data=mysql_fetch_object($result)){
?>
['<?=$data->nama;?>', <?=$data->lat2;?>, <?=$data->lng2;?>],
<?
}
?>
];
myOptions = {
zoom: 12,
center: pointA,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
map = new google.maps.Map(document.getElementById('map'), myOptions),
ser = new google.maps.DirectionsService,
ren = new google.maps.DirectionsRenderer({
map: map
});
<?php
$result = mysql_query("SELECT * FROM lokasi");
while ($row = mysql_fetch_array($result))
// foreach($result as $row) // <- remove this line
echo "addMarker(new google.maps.LatLng(".$row['lat'].", ".$row['lng']."), map),
addMarker2(new google.maps.LatLng(".$row['lat2'].", ".$row['lng2']."), map);";
?>
for (i = 0; i < locations.length; i++) {
pointA = new google.maps.LatLng(locations[i][1], locations[i][2])
};
for (j = 0; j < locations2.length; j++) {
pointB = new google.maps.LatLng(locations2[j][1], locations2[j][2])
};
// get route from A to B
calculateAndDisplayRoute(ser, ren, pointA, pointB);
}
function calculateAndDisplayRoute(ser, ren, pointA, pointB) {
ser.route({
origin: pointA,
destination: pointB,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
ren.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
function addMarker(pointA, map) {
var marker = new google.maps.Marker({
position: pointA,
map: map,
icon: 'seru.png',
animation: google.maps.Animation.BOUNCE
});
//show INFOWINDOW
var contentString =
'<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h3 id="firstHeading" class="firstHeading">START POINT</h3>'+
'<div id="bodyContent">'+
'<p>I want to show this with locations[i].lat and locations[1].lng</p>'+
'<p>Web <a href="Facebook">'+
'www.facebook.com</a> .</p>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
function addMarker2(pointB, map, j) {
var marker2 = new google.maps.Marker({
position: pointB,
map: map,
icon: 'seru 2.png',
animation: google.maps.Animation.DROP
});
//show INFOWINDOW
var contentString =
'<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h3 id="firstHeading" class="firstHeading">END POINT</h3>'+
'<div id="bodyContent">'+
'<p>I want to show this with locations[j].lat2 and locations[j].lng2</p>'+
'<p>Web <a href="Facebook">'+
'www.facebook.com</a> .</p>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker2, 'click', function() {
infowindow.open(map,marker2);
});
这是从数据库中显示两个标记的代码:
<?php
$result = mysql_query("SELECT * FROM lokasi (idlokasi,nama,lat,lng,lat2,lng2");
while ($row = mysql_fetch_array($result))
// foreach($result as $row) // <- remove this line
echo "addMarker(new google.maps.LatLng(".$row['lat'].", ".$row['lng']."), map),
addMarker2(new google.maps.LatLng(".$row['lat2'].", ".$row['lng2']."), map);";
var pointA = new google.maps.LatLng(".$row['lat'].", ".$row['lng'].")
var pointB = new google.maps.LatLng(".$row['lat2'].", ".$row['lng2'].")
?>
【问题讨论】:
-
您在添加其他路线时遇到什么问题?请提供一个 Minimal, Complete, Tested and Readable example 来证明您的问题。
-
google.maps.LatLng 对象没有“点击”事件。
-
我添加了更多代码,我的问题是如何从我拥有的所有标记中显示路线方向?因为我只显示来自 1 个数据标记的 1 个路线方向。
-
“google.maps.LatLng 对象没有‘点击’事件”是什么意思?我必须删除它还是什么?
-
发布的代码没有定义 any 标记,并且发布的代码将“点击”侦听器添加到 google.maps.LatLng 对象(pointA、pointB)不要做任何有用的事情。请提供一个 Minimal, Complete, Tested and Readable 示例来说明您的问题。
标签: javascript php mysql google-maps google-maps-api-3