I\'m making a google maps project and need to create a route clicking on it.
My project has 2 points that has a predefined lat and lng, and I want to draw by myself
Concept: (sounds like this is what you want)
(if this what you are trying and are running into problems, code or a live link would be helpful)
You are missing #3 and #4
Working example
code snippet:
var map, ren, ser;
var data = {};
var data2 = {};
var marker;
var infowindow;
var doMark = true;
var directionsDisplay;
var wayA;
var wayB;
//Função de Inicio
function goma() {
var mapDiv = document.getElementById('mappy');
var mapOptions = {
zoom: 12,
center: new google.maps.LatLng(-23.563594, -46.654239),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
//Cria o mapa do google, coloca as definições do mapa, como tipo de visualização, pode ser ROADMAP, SATELLITE, HYBRID, TERRAIN
map = new google.maps.Map(mapDiv, mapOptions);
var control = document.createElement('DIV');
control.style.padding = '1px';
control.style.border = '1px solid #000';
control.style.backgroundColor = 'white';
control.style.cursor = 'pointer';
control.innerHTML = '
';
control.index = 1;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(control);
google.maps.event.addDomListener(control, 'click', function() {
doMark = false;
markNow();
});
google.maps.event.addListener(map, "click", function(event) {
if (!wayA) {
wayA = new google.maps.Marker({
position: event.latLng,
map: map
});
} else {
wayB = new google.maps.Marker({
position: event.latLng,
map: map
});
//Renderiza a rota, o draggable é diz se o waypoint é arrastavel ou não
ren = new google.maps.DirectionsRenderer({
'draggable': true
});
ren.setMap(map);
ren.setPanel(document.getElementById("directionsPanel"));
ser = new google.maps.DirectionsService();
//Cria a rota, o DirectionTravelMode pode ser: DRIVING, WALKING, BICYCLING ou TRANSIT
ser.route({
'origin': wayA.getPosition(),
'destination': wayB.getPosition(),
'travelMode': google.maps.DirectionsTravelMode.DRIVING
}, function(res, sts) {
if (sts == 'OK') ren.setDirections(res);
})
}
});
}
var html = "" +
"Nome: " +
"Endereco: " +
"Tipo: " +
" ";
infowindow = new google.maps.InfoWindow({
content: html
});
google.maps.event.addDomListener(window, 'load', goma);
html,
body {
height: 100%;
width: 100%;
}
讨论(0)
- 热议问题