¿Rutas optimas, php + Api GoogleMaps?
Publicado por Jorge (3 intervenciones) el 25/01/2021 19:55:29
Hola buenas, estoy trabajando con la Api de GoogleMaps, logré conectar la base de datos logrando mostrar marcadores con su respectiva latitud y longitud en el mapa, mi duda es: Ya generando aquello ¿Cómo puedo implementar directions services o waypoints para poder unir los marcadores con una ruta optima?, llevo tiempo investigando y implementando pero no me resulta. Agradecería mucho una ayudita o algún ejemplo práctico. Saludos. Adjunto el código, esto funciona bien.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
// Datos para la conexion
$host = 'localhost';
$database = 'nombre_database';
$username = 'root';
$password = ' ';
// Conectarse a MySQL
$link = mysql_connect($host, $username, $password);
if (!$link) {
echo('Error al conectarse a mysql: ' . mysql_error());
}
// Seleccionar nuestra base de datos
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
echo ('Error al abrir la base de datos: ' . mysql_error());
}
else {
echo 'Conexion exitosa.';
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Maps</title>
<style type="text/css">
body { font: normal 10pt Helvetica, Arial; }
#map { width: 100%;
height: 500px;
border: 1px solid #000;
float: left; }
</style>
<script src="http://maps.google.com/maps/api/js?key=XXXXXXX" type="text/javascript"></script>
<script type="text/javascript">
//icono del marker con sus propiedades
var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/yellow.png",
new google.maps.Size(32, 32), new google.maps.Point(0, 0),
new google.maps.Point(16, 32));
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info) { // Se agrega marcador
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
// mensaje arriba del marcador
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
// para que aparezca dando click al marcador
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
// para cerrar el mensaje
google.maps.event.addListener(popup, "closeclick", function() {
map.panTo(center);
currentPopup = null;
});
} // Se inicia el mapa
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(0, 0),
zoom: 14,
// barra de tipo de mapas a mostrar
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.ZOOM_PAN
}
});
// se hace la consulta a la base de datos tomando los datos de lat y lng
<?php
$query = mysql_query("SELECT * FROM suscriptor WHERE estado = '0'")or die(mysql_error());
while($row = mysql_fetch_array($query))
{
$name = $row['nombre'];
$lat = $row['latitud'];
$lon = $row['longitud'];
echo("addMarker($lat, $lon, '<b>$name</b><br />');\n");
}
?>
center = bounds.getCenter();
map.fitBounds(bounds);
}
</script>
</head>
<body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
<div id="map"></div>
<div id="botones">
<br/>
<button id="ruta" onclick="funcionRuta()">Ruta</button>
</div>
</body>
</html>
Valora esta pregunta
0