hello ;
You can use This method to get the distance between two point :
var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
or you can create a polyline from your points then you apply that method
:kilometer = polyline.getLength() / 1000;
after that you can insert your value to teh database or what ever you want :D.
regards..
abpteam <[email protected]> wrote:
>
> Hello everybody!
>
> I'm trying to retrieve some coordinates (only one lat and one lng)
> from my database and calculate the distance between those coordinates
> and the new coordinates that I'm saving with a draggable marker. After
> that I want to save this distance to my database
>
> here is the files that I'm using:
>
> index.php
> ****************
> <?php
>
>
> include('phpsqlinfo_dbinfo.php');
> // Opens a connection to a MySQL server
> $connection = mysql_connect ("mysql.spatiul.ro", $username,
> $password);
> if (!$connection) {
> die('Not connected : ' . mysql_error());
> }
>
> // Set the active MySQL database
> $db_selected = mysql_select_db($database, $connection);
> if (!$db_selected) {
> die ('Can\'t use db : ' . mysql_error());
> }
>
>
> $query1 = sprintf("Select clat from pics where id=1");
> $result1 = mysql_query($query);
> $query2 = sprintf("Select clng from pics where id=1");
> $result2 = mysql_query($query);
>
> ?>
>
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
>
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-
> microsoft-com:vml">
> <head>
> <meta http-equiv="content-type" content="text/html; charset=utf-8"/
> >
> <title>Testez Google Maps</title>
> <link href="styles.css" rel="stylesheet" type="text/css"
> media="screen" />
> <p> </p><p> </p><p> </p><p> </p><p> </
> p><p> </p><p> </p><p> </p>
> <script src="http://maps.google.com/maps?
>
file=api&v=2&key=ABQIAAAAFaVhe_23DsL3xUvBVa7J7xTyw7y9fKY6L1WvbiDg859fbFgy7RSDw30FoDtX6H03QfTsKI1TLsSl8Q&sensor=false"
> type="text/javascript"></script>
>
> <script type="text/javascript">
>
> var marker;
>
> function initialize() {
> if (GBrowserIsCompatible()) {
> var map = new GMap2(document.getElementById("map_canvas"));
> map.setCenter(new GLatLng(45.5, 25.5), 3);
> map.addControl(new GLargeMapControl());
> var mapControl = new GMapTypeControl();
> map.addControl(mapControl);
> //map.addControl(new GSmallMapControl());
> //map.addControl(new GMapTypeControl());
>
> // Create a base icon for all of our markers that specifies
> the
> // shadow, icon dimensions, etc.
> var baseIcon = new GIcon();
> baseIcon.shadow = "http://www.google.com/mapfiles/
> shadow50.png";
> baseIcon.iconSize = new GSize(20, 34);
> baseIcon.shadowSize = new GSize(37, 34);
> baseIcon.iconAnchor = new GPoint(9, 34);
> baseIcon.infoWindowAnchor = new GPoint(9, 2);
> baseIcon.infoShadowAnchor = new GPoint(18, 25);
> baseIcon.printImage="http://www.google.com/mapfiles/
> markerie.gif";
> baseIcon.mozPrintImage="http://www.google.com/mapfiles/
> markerff.gif";
> baseIcon.printShadow="http://www.google.com/mapfiles/
> dithshadow.gif";
> // baseIcon.transparent="http://www.google.com/mapfiles/
> markerTransparent.png";
> // baseIcon.imageMap=
>
[9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0];
>
> // Creates a marker whose info window displays the letter
> corresponding
> // to the given index.
> function createMarker(point, index) {
> // Create a lettered icon for this point using our icon
> class
> var letter = String.fromCharCode("A".charCodeAt(0) + index);
> var letteredIcon = new GIcon(baseIcon);
> letteredIcon.image = "http://www.google.com/mapfiles/marker"
> + letter + ".png";
>
> // Set up our GMarkerOptions object
> markerOptions = { icon:letteredIcon, draggable:true };
> marker = new GMarker(point, markerOptions);
>
> GEvent.addListener(marker, "click", function() {
> marker.openInfoWindowHtml("Place the Marker in the right
> position!");
> });
>
> GEvent.addListener(marker, "dragstart", function() {
> //map.closeInfoWindow();
> marker.openInfoWindowHtml("Place the Marker in the
> right
> position!");
> });
>
> GEvent.addListener(marker, "dragend", function() {
> marker.openInfoWindowHtml("Is this the right position? Then
> press Save&Close!");
> });
> return marker;
> }
>
> // Add 10 markers to the map at random locations
> /*var bounds = map.getBounds();
> var southWest = bounds.getSouthWest();
> var northEast = bounds.getNorthEast();
> var lngSpan = northEast.lng() - southWest.lng();
> var latSpan = northEast.lat() - southWest.lat();
> //for (var i = 0; i < 10; i++) {
> var latlng = new GLatLng(southWest.lat() + latSpan *
> Math.random(),
> southWest.lng() + lngSpan * Math.random());
> */
> var latlng = new GLatLng(45.5,25.5);
> map.addOverlay(createMarker(latlng, 1));
>
> //}
> }
> }
>
> function saveData() {
> var latlng = marker.getLatLng();
> var lat = latlng.lat();
> var lng = latlng.lng();
>
> var url = "phpsqlinfo_addrow.php?
> name=Nume&address=Adresa&type=Bar&lat=" + lat + "&lng=" + lng;
> GDownloadUrl(url, function(data, responseCode) {
> if (responseCode == 200 && data.length <= 1) {
> marker.closeInfoWindow();
> document.getElementById("message").innerHTML = "Your
> selection was saved.";
> }
>
> });
> var Gpoint1= new Glatlng(<?php echo($result1); ?>, <?php echo
> ($result2); ?>);
> var Gpoint2= new Glatlng(lat ,lng);
> var distance = Gpoint1.distanceFrom(Gpoint2);
> var url1 = "intdist.php?lat=" + lat + "&lng=" + lng + "&dist=" +
> distance;
> GDownloadUrl(url1, function(data, responseCode) {
> if (responseCode == 200 && data.length <= 1) {
> marker.closeInfoWindow();
> document.getElementById("message").innerHTML = "Distanta:"+
> distance;
> }
> });
> }
> </script>
> </head>
>
>
> <body onload="initialize()" onunload="GUnload()">
>
>
> <div id="content_bg">
> <div id="content">
> <!-- header begins -->
>
>
>
> <!-- header ends -->
> <!-- content begins -->
> <div id="main">
> <!--content ends -->
> <!--footer begins -->
> </div>
>
> <div id="footer">
> <p> </p>
>
>
> <p> </p>
> <p> </p>
> <center><p> </p><p> </p> <div id="map_canvas" style="width:
> 600px; height: 500px"></div> <p> </p>
>
>
> <input type="button" value="Save&Close" onclick="saveData()" /
> ><p> </p>
> <div id="message"></div></center>
> <p> </p>
> <p> </p>
>
>
> </div>
> </div>
> </div>
>
> </body>
> </html>
> **************
> **************
> **************
> phpsqlinfo_dbinfo.php
> **************
>
> <?php
> $username="user";
> $password="pass";
> $database="database";
> ?>
>
>
> *****************
> *****************
> *****************
> phpsqlinfo_addrow.php
> *****************
>
> <?php
> //require("phpsqlinfo_dbinfo.php");
> // Opens a connection to a MySQL server
> $connection = mysql_connect ("myserver", $username, $password);
> if (!$connection) {
> die('Not connected : ' . mysql_error());
> }
>
> // Set the active MySQL database
> $db_selected = mysql_select_db($database, $connection);
> if (!$db_selected) {
> die ('Can\'t use db : ' . mysql_error());
> }
>
>
>
> // Gets data from URL parameters
> $name = $_GET['name'];
> $address = $_GET['address'];
> $lat = $_GET['lat'];
> $lng = $_GET['lng'];
> $type = $_GET['type'];
>
>
>
> // Insert new row with user data
> $query = sprintf("INSERT INTO markers " .
> " (id, name, address, lat, lng, type ) " .
> " VALUES (NULL, '%s', '%s', '%s', '%s', '%s');",
> mysql_real_escape_string($name),
> mysql_real_escape_string($address),
> mysql_real_escape_string($lat),
> mysql_real_escape_string($lng),
> mysql_real_escape_string($type));
>
> $result = mysql_query($query);
>
> if (!$result) {
> die('Invalid query: ' . mysql_error());
> }
>
> ?>
> *****************
> *****************
> *****************
> intdist.php
> *****************
> <?php
>
> $lat = $_GET['lat'];
> $lng = $_GET['lng'];
> $distance = $_GET['dist'];
>
> $db->query("Update markers set error=$distance where lat=$lat and lng=
> $lng");
>
> ?>
>
> *****************
>
>
>
> I'm doing my tests at www.mafiotul.ro/geo/index.php
> Where am I mistaking?
> Can you help me?
>
> Thank you,
>
> Adrian
>
> >
>
Tamer Hatoum;
Web Developer and Programmer;
Qatar Navigator Est;
Tel: +974 6026267;
[email protected]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Maps API" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Maps-API?hl=en
-~----------~----~----~----~------~----~------~--~---