falent
06-24-2012, 06:38 PM
How should I add a variable "distance" to a filed "odleglosc" in a form ? I tried document.getElementById("distance").value = results[0].distance.text; but without success :(
http://celero.pl/Atol/inny.php
<html>
<head>
<LINK rel="stylesheet" HREF="style1.css" TYPE="text/css">
<title>Google Maps Distance Matrix | http://sharp-coders.blogspot.com</title>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<style type="text/css">
body {
margin: 20px;
font-family: courier, sans-serif;
font-size: 12px;
}
#map {
height: 480px;
width: 640px;
border: solid thin #333;
margin-top: 20px;
}
</style>
<script type="text/javascript">
var map;
var geocoder;
var bounds = new google.maps.LatLngBounds();
var markersArray = [];
//var origin2 = new google.maps.LatLng(32.5, 72.5);
var origin1 = document.getElementById("origins").value;
var destinationA = document.getElementById("destinations").value;
//var destinationB = new google.maps.LatLng(32.5, 72.5);
var destinationIcon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000";
var originIcon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000";
function initialize() {
var opts = {
center: new google.maps.LatLng(52.406451,16.917958),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), opts);
geocoder = new google.maps.Geocoder();
}
function calculateDistances() {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [document.getElementById("origins").value],
destinations: [document.getElementById("destinations").value],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
// visit http://code.google.com/apis/maps/documentation/javascript/tutorial.html for more configuration options
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = document.getElementById('outputDiv');
outputDiv.innerHTML = '';
deleteOverlays();
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
addMarker(origins[i], false);
for (var j = 0; j < results.length; j++) {
addMarker(destinations[j], true);
outputDiv.innerHTML += results[j].distance.text +"<br />";
}
document.getElementById("distance").value = results[0].distance.text;
}
}
}
function addMarker(location, isDestination) {
var icon;
if (isDestination) {
icon = destinationIcon;
} else {
icon = originIcon;
}
geocoder.geocode({'address': location}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: icon
});
markersArray.push(marker);
} else {
alert("Geocode was not successful for the following reason: "
+ status);
}
});
}
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
</script>
</head>
<body onload="initialize()">
<textarea id="origins" class="database">Aleja Niepodleglosci, Poznan</textarea><br />
Destinations:<br />
<textarea id="destinations"></textarea><br />
<p><button type="button" onclick="calculateDistances();">Oblicz odleglosc</button></p>
<br/>
Odleglosc:
<input type="text" id="distance"/>
<div id="outputDiv"></div>
<div id="map"></div>
</body>
</html>
http://celero.pl/Atol/inny.php
<html>
<head>
<LINK rel="stylesheet" HREF="style1.css" TYPE="text/css">
<title>Google Maps Distance Matrix | http://sharp-coders.blogspot.com</title>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<style type="text/css">
body {
margin: 20px;
font-family: courier, sans-serif;
font-size: 12px;
}
#map {
height: 480px;
width: 640px;
border: solid thin #333;
margin-top: 20px;
}
</style>
<script type="text/javascript">
var map;
var geocoder;
var bounds = new google.maps.LatLngBounds();
var markersArray = [];
//var origin2 = new google.maps.LatLng(32.5, 72.5);
var origin1 = document.getElementById("origins").value;
var destinationA = document.getElementById("destinations").value;
//var destinationB = new google.maps.LatLng(32.5, 72.5);
var destinationIcon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000";
var originIcon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000";
function initialize() {
var opts = {
center: new google.maps.LatLng(52.406451,16.917958),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), opts);
geocoder = new google.maps.Geocoder();
}
function calculateDistances() {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [document.getElementById("origins").value],
destinations: [document.getElementById("destinations").value],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
// visit http://code.google.com/apis/maps/documentation/javascript/tutorial.html for more configuration options
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = document.getElementById('outputDiv');
outputDiv.innerHTML = '';
deleteOverlays();
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
addMarker(origins[i], false);
for (var j = 0; j < results.length; j++) {
addMarker(destinations[j], true);
outputDiv.innerHTML += results[j].distance.text +"<br />";
}
document.getElementById("distance").value = results[0].distance.text;
}
}
}
function addMarker(location, isDestination) {
var icon;
if (isDestination) {
icon = destinationIcon;
} else {
icon = originIcon;
}
geocoder.geocode({'address': location}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: icon
});
markersArray.push(marker);
} else {
alert("Geocode was not successful for the following reason: "
+ status);
}
});
}
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
</script>
</head>
<body onload="initialize()">
<textarea id="origins" class="database">Aleja Niepodleglosci, Poznan</textarea><br />
Destinations:<br />
<textarea id="destinations"></textarea><br />
<p><button type="button" onclick="calculateDistances();">Oblicz odleglosc</button></p>
<br/>
Odleglosc:
<input type="text" id="distance"/>
<div id="outputDiv"></div>
<div id="map"></div>
</body>
</html>