Avatar billede Hecro Nybegynder
17. januar 2010 - 13:49 Der er 3 kommentarer og
1 løsning

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")%>&nbsp;<%=ListAdresse1("Husnummer")%>&nbsp;<%=ListAdresse1("Postnr")%>&nbsp;<%=ListAdresse1("By")%>" />
        <input type="hidden" name="address2" value="<%=ListAdresse2("Vejnavn")%>&nbsp;<%=ListAdresse2("Husnummer")%>&nbsp;<%=ListAdresse2("Postnr")%>&nbsp;<%=ListAdresse2("By")%>" />
        <input type="submit" name="find" value="Search" />
      </p>
    </form>
    <% Response.Write "<p id=results></p>"%>
  </body>
</html>
Avatar billede Hecro Nybegynder
17. januar 2010 - 14:12 #1
Ingen bud?
Avatar billede Hecro Nybegynder
17. januar 2010 - 21:03 #2
Er der virkelig ikke nogle der har en løsning?
Avatar billede Hecro Nybegynder
17. januar 2010 - 22:29 #3
Har lige ændret lidt på koden - mangler stadig at få postet resultatet til databasen. Der MÅ være en der kan hjælpe mig.

NY KILDEKODE:

<%
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/www.FJERNET.dk/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") = ("HVAD SKAL JEG INDSÆTTE HER??")
    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" id="Form29" name=form1>
        <SELECT id="SELECT29" name=address1>           
            <%
            Set DataConn = Server.CreateObject("ADODB.Connection")
            DSN = "DRIVER={Microsoft Access Driver (*.mdb)};uid=admin; "
            DSN = dsn & "DBQ=C:/Data/www.FJERNET.dk/db/db.mdb"
            DataConn.Open DSN

            Set StedA = Server.CreateObject("ADODB.RecordSet")
            StedA.open "SELECT * FROM [Adresser] Where BrugerID = " & ("1") & " ORDER BY StedNavn ASC", DataConn
            if not StedA.EOF then
            StedA.MoveFirst
            end if
            do while not StedA.EOF
           
            StringAdresse1 = StedA("Vejnavn") & " " & StedA("Husnummer") & ", " & StedA("Postnr") & " " & StedA("By")
            %>
           
            <OPTION value="<%=StringAdresse1%>" <%if StedA("Standard") = TRUE then%>selected<%end if%>><%=StedA("StedNavn")%></OPTION>               
           
            <%
            StedA.MoveNext
            loop
            StedA.Close
            set StedA = nothing
            Dataconn.Close
            %>
        </SELECT>
        ->
        <SELECT id="SELECT1" name=address2 onclick="showLocation(); return false;">     
            <%
            Set DataConn = Server.CreateObject("ADODB.Connection")
            DSN = "DRIVER={Microsoft Access Driver (*.mdb)};uid=admin; "
            DSN = dsn & "DBQ=C:/Data/www.FJERNET.dk/db/db.mdb"
            DataConn.Open DSN

            Set StedB = Server.CreateObject("ADODB.RecordSet")
            StedB.open "SELECT * FROM [Adresser] Where BrugerID = " & ("1") & " ORDER BY StedNavn ASC", DataConn
            if not StedB.EOF then
            StedB.MoveFirst
            end if
            do while not StedB.EOF
           
            StringAdresse2 = StedB("Vejnavn") & " " & StedB("Husnummer") & ", " & StedB("Postnr") & " " & StedB("By")
            %>
           
            <OPTION value="<%=StringAdresse2%>"><%=StedB("StedNavn")%></OPTION>               
           
            <%
            StedB.MoveNext
            loop
            StedB.Close
            set StedB = nothing
            Dataconn.Close
            %>
        </SELECT>
        <INPUT TYPE="submit" class="art.button" value="Opret afstand" id="Submit29" name=submit2>
    </form>
    <br />
    <b>Afstand:</b>
    <p id=results></p>
  </body>
</html>
Avatar billede Hecro Nybegynder
17. januar 2010 - 23:14 #4
Har løst problemet selv
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester