Reputation: 87
I use the code below. I try to create a map with a marker and an InfoWindow in which I would like to have a textfield to write an address and get the direction from that address to marker. I would like, when you write an address in the text field appear above suggested places from google. I hope that I explain nice my problem and someone help me.
var geocoder;
var directionsService;
var directionsDisplay;
var latlng = new google.maps.LatLng(37.396746, 21.677257);
var infowindow = new google.maps.InfoWindow({
content:"<center><strong>Apollo Camping</strong></center><br/>"+
"<center>Πως θα έρθετε</center>"+
"Διεύθυνση: <input id='clientAddress' type='text' onFocus='geolocate()' placeholder='Εισάγετε τη διεύθυνσή σας' size=22>"+
"<center><input type='button' onClick=getDir() value='Λήψη οδηγιών'></center>"
});
function initialize() {
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: 'Atlanta/Sandy Springs',
clickable: true
});
infowindow.open(map, marker);
google.maps.event.addListener(marker, 'click', function () {
});
geocoder = new google.maps.Geocoder();
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer({
suppressMarkers: false
});
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}
function getDir() {
geocoder.geocode({
'address': document.getElementById('clientAddress').value
},
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var origin = results[0].geometry.location;
var destination = latlng;
var request = {
origin: origin,
destination: destination,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
} else {
document.getElementById('clientAddress').value =
"Directions cannot be computed at this time.";
}
});
}
initialize();
This is the javascript and I call it in the html with . I'm waiting your reply and I hope that somebody can understand what I mean.
Thank you all in advance!
Upvotes: 0
Views: 341
Reputation: 117334
#clientAddress
create the autocomplete on domready of the infowindow:
google.maps.event.addListener(infowindow,'domready',function(){
//create the autocomplete
var ac=new google.maps.places
.Autocomplete(document.getElementById('clientAddress'));
//run getDir when the place changes
google.maps.event.addListener(ac, 'place_changed', getDir);
});
Upvotes: 1