Witam, znalazłem w api google cos czego szukalem.. ale nie daje sobie rady z java, bo jestem w niej zielony, nie znam sie na tym szczerze mowiac. A potrzebuje z kodu wyłuskać jedynie CZAS, miedzy dwoma miejscami.
Nie potrzebna mi jest mapa, ani markery pokazywane na niej... tylko skąd dokąd i CZAS!
stronka o api google: http://code.google.com/intl/pl-PL/apis/maps/documentation/javascript/services.html#distance_matrix
A ponizej kod z przykladu.
Gdy sam usuwam jakieś rzeczy to wtedy nic mi się nie uruchamia, gdy pokasowałem tą tablice dwuwymiarowa to tez mi się nic nie wyświetlało po naciśnięciu.
potrzebuje tylko jednego orgin i destinations. przykładowo var origin = "Lodz, Poland";
var destinations = "Warszawa, Poland";
później będę myślał jak wyłuskać z Javy ten czas jako zmienną do php. ale to później będę się o to martwił.
Czy ktoś może wie co z tym zrobić?
<html>
<head>
<title>Google Maps API v3 Example: Distance Matrix</title>
<script src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
</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>
var map;
var geocoder;
var bounds = new google.maps.LatLngBounds();
var markersArray = [];
var origin1 = new google.maps.LatLng(55.930385, -3.118425);
var origin2 = "Lodz, Poland";
var destinationA = "Warszawa, Poland";
var destinationB = new google.maps.LatLng(50.087692, 14.421150);
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(55.53, 9.4),
zoom: 100,
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: [origin1, origin2],
destinations: [destinationA, destinationB],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
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 += origins[i] + " to " + destinations[j]
+ ": " + results[j].distance.text + " in "
+ results[j].duration.text + "<br />";
}
}
}
}
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()">
<div id="inputs">
<pre class="prettyprint">
var origin1 = new google.maps.LatLng(55.930385, -3.118425);
var origin2 = "Greenwich, England";
var destinationA = "Stockholm, Sweden";
var destinationB = new google.maps.LatLng(50.087692, 14.421150);
</pre>
<p><button type="button" onclick="calculateDistances();">Calculate
distances</button></p>
</div>
<div id="outputDiv"></div>
<div id="map"></div>
</body>
</html>
Chyba wszyscy pracują:) W każdym razie coś porobiłem.
A wiec już mam punkt startowy, docelowy i pokazuje mi wynik.
Teraz bym chciał zrobić coś takiego ze w miejsce var destinationA = "Piotrkowska 250, Lodz, Poland"; wpisywało się dynamicznie coś ze strony. Żeby to było od php jakaś zmienna.. a wynikiem tez z zmiennej php był CZAS.
Ale nie wiem jak wyłuskać to.
Czy ktoś mi pomoże?
<html>
<head>
<title>Google Maps API v3 Example: Distance Matrix</title>
<script src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
</script>
<style type="text/css">
body {
margin: 20px;
font-family: courier, sans-serif;
font-size: 12px;
}
</style>
<script>
var origin1 = "Piotrkowska 60, Lodz, Poland";
var destinationA = "Piotrkowska 250, Lodz, Poland";
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 calculateDistances() {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin1],
destinations: [destinationA],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
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 = '';
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
outputDiv.innerHTML += origins[i] + " to " + destinations[j]
+ ": " + results[j].distance.text + " in "
+ results[j].duration.text + "<br />";
}
}
}
}
</script>
</head>
<body onload="initialize()">
<div id="inputs">
<pre class="prettyprint">
var origin1 = "Piotrkowska 60, Lodz, Poland";
var destinationA = "Piotrkowska 250, Lodz, Poland";
</pre>
<p><button type="button" onclick="calculateDistances();">Calculate
distances</button></p>
</div>
<div id="outputDiv"></div>
<div id="map"></div>
</body>
</html>