Javascript + ASP (Skriv Javascript resultat til databasen)
Jeg har fundet et javascript som udnytter Google Maps API til at hente afstanden imellem 2 adresser.Nu er problemet bare at få skrevet denne afstanden til databasen.
Kildekode:
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & ("C:/Data/FJERNET/db/db.mdb")'
sql = "SELECT * FROM Adresser where ID= " & ("7")
set ListAdresse1 = Conn.Execute(sql)
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & ("C:/Data/FJERNET/db/db.mdb")'
sql = "SELECT * FROM Adresser where ID= " & ("1")
set ListAdresse2 = Conn.Execute(sql)
if Request.QueryString("action") = ("opret") then
Set DataConn = Server.CreateObject("ADODB.Connection")
DSN = "DRIVER={Microsoft Access Driver (*.mdb)};uid=admin; "
DSN = dsn & "DBQ=C:/Data/FJERNET/db/test.mdb"
DataConn.Open DSN
Set Record = Server.CreateObject("ADODB.RecordSet")
Record.open "SELECT * FROM [JS] ORDER BY id", DataConn, 1, 3
Record.AddNew
Record.Fields("Afstand") = "<p id=results></p>"
Record.Update
Record.Close
set Record = nothing
Dataconn.Close
Response.Redirect("googlemaps.asp")
end if
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAALivP3hZEpzf8e7G_RMS46xTBI9abX6mjwQhBDEzC185cnRmR1BRl2SDO6Ctt7UEs3XiG5xyxg4ZFzA" type="text/javascript"></script>
<script type="text/javascript">
var geocoder, location1, location2, gDir;
function initialize() {
geocoder = new GClientGeocoder();
gDir = new GDirections();
GEvent.addListener(gDir, "load", function() {
var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
document.getElementById('results').innerHTML = drivingDistanceKilometers;
});
}
function showLocation() {
geocoder.getLocations(document.forms[0].address1.value, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the first address");
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(document.forms[0].address2.value, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the second address");
}
else
{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
gDir.load('from: ' + location1.address + ' to: ' + location2.address);
}
});
}
});
}
</script>
</head>
<body onload="initialize()">
<form method="post" action="googlemaps.asp?action=opret" onsubmit="showLocation(); return false;">
<p>
<input type="hidden" name="address1" value="<%=ListAdresse1("Vejnavn")%> <%=ListAdresse1("Husnummer")%> <%=ListAdresse1("Postnr")%> <%=ListAdresse1("By")%>" />
<input type="hidden" name="address2" value="<%=ListAdresse2("Vejnavn")%> <%=ListAdresse2("Husnummer")%> <%=ListAdresse2("Postnr")%> <%=ListAdresse2("By")%>" />
<input type="submit" name="find" value="Search" />
</p>
</form>
<% Response.Write "<p id=results></p>"%>
</body>
</html>