• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1907
  • Last Modified:

Google maps Lat,Lon value by address

Hello I am busy with a project where i have to store data in the database on lat, lon values.

I need a simple script which displays the lat and lon values by a function call value like

function showLatLon(address)
{
 ................
 alert(lat,lon);
}

.....


showLatLon("Wellekenstraat 29, 9300 aalst, belgium");

The fact is, it should be just a function call not a button event like onclick();

thanks in advance.
0
Ludo_Dirckx
Asked:
Ludo_Dirckx
  • 4
  • 2
1 Solution
 
b0lsc0ttCommented:
In your tags you mention Google Maps.  Do you have a key and plan to use Google Maps as a source for this?  If not what?
What language does this function need to be in?  Javascript was a tag but please confirm or correct.  You mentioned a database so I wonder if Javascript is really what you need.
Because of some conflicting things you mentioned it would be nice to have an idea of how this will be used and where.  It can be an important detail and it would prove to be a real waste of time providing Javascript if you need something to run in server code to update your DB.
bol
0
 
Ludo_DirckxAuthor Commented:
Hello b0lsc0tt,
Here the description how it will be used.
The page is php.
I am trying to make a site which is using the lat and lon values to show some addresses.
There are some ideal solutions to work the way how i want eg http://www.phpinsider.com/php/code/GoogleMapAPI/demo/

but the mail problem for me is i do not get the code working on our server because of some proxy limitation the code is not working.

I always get an error on file_get_contents() function.
i checked my php_ini() file and  allow_url_fopen is on.

I checked with one of our site with file_get_contents() test and it is working.

so i am trying to do it other way with the code which is working on our server.

Of course i do have a google key.

So now i need a simple function that converts the address to lat, lon values and i am going to store the values in mysql server later on in filemaker. Because actually this project will combine filemaker with php.

For me the only thing now needed is a simple function that converts address to lat, lon values.

it can be php or javascript .

thanks in advance.


0
 
b0lsc0ttCommented:
Thanks for the response and info.  Can you use CURL?  The page below works for allowing input that will then get the Lat & Long (plus other things) from Google.  I am not real clear on your issue with file_get_contents() so it may affect CURL too but it should be easy to test and try with the page below.  If that works then you can modify the code to put it in a function or use how you want.  I will be glad to help if you need it.
Let me know how it works or if you have a question.  I included notes and comments so it should be pretty clear.
bol

<?php
/*
To access the Maps API geocoder directly using server-side scripting, send a request to http://maps.google.com/maps/geo? with the following parameters in the URI:
 
    * q -- The address that you want to geocode.
    * key -- Your API key.
    * output -- The format in which the output should be generated. The options are xml, kml, csv, or json.
E.g.  http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=xml&key=abcdefg
*/
$result = "";
$strAddress = "";
if (isset($_POST["completeAddress"])) {
	
	$strAddress = $_POST['completeAddress'];
	$url = "http://maps.google.com/maps/geo?"; // base URL
	$url .= "q=" . urlencode($strAddress);
	$url .= "&output=json";
	$url .= "&key=YOURKEY";
	
	$content = null;
	$ch = curl_init();
	
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$content = curl_exec($ch);
	curl_close($ch);
	
	if ($content === null) die ("Could not get the page.");
	
	$result = htmlspecialchars($content);
}
?>
 
<!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" xml:lang="en" lang="en">
<head>
<link href="/style.css" rel="stylesheet" type="text/css" />
<title>Geocode an address</title>
<script type="text/javascript">
function init() {
	document.form1.completeAddress.onfocus = function () {
		if (this.value != '') {
			this.value = '';
			document.getElementById('display').innerHTML = '';
		}
	}
}
</script>
</head>
 
<body onload="init();">
<h3>Geocode an address</h3>
 
<br /><br />
<form name="form1" action="" method="post">
Enter adddress to be encoded: <input type="text" name="completeAddress" value="<?php echo $strAddress; ?>" size="60" />
&nbsp;&nbsp;&nbsp;<a href="#" onclick="document.form1.completeAddress.focus(); return false">edit address</a>
<br />
E.g. 123 Main St, Downtown, CA, 91111
<br /><br />
<input type="submit" value="Geocode" />
</form>
 
<br /><br /><br />
Result:<br />
<div id="display">
<?php echo $result; ?>
</div>
 
 
</body>
</html>
 
<?php
/*
returns a JSON object consisting of the following information:
 
    * Status
    *
          o request -- The request type. In this case, it is always geocode.
          o code -- A response code (similar to HTTP status codes) indicating whether the geocode request was successful or not. See the full list of status codes.
    * Placemark -- Multiple placemarks may be returned if the geocoder finds multiple matches.
    *
          o address -- A nicely formatted and properly capitalized version of the address.
          o AddressDetails -- The address formatted as xAL, or eXtensible Address Language, an international standard for address formatting.
          o
                + Accuracy -- An attribute indicating how accurately we were able to geocode the given address. See a list of possible values.
          o Point -- A point in 3D space.
          o
                + coordinates -- The longitude, latitude, and altitude of the address. In this case, the altitude is always set to 0.
*/
?>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ludo_DirckxAuthor Commented:
Hi Bol,
Thanks for the code sample.
I tried the code and it is still not working.
I thing there is some kind of port blocking in our server or url filtering blocks to get the specific url.
I tried to put the url as cleared but still there is a problem.
I thing it is port blocking.
If you know there is a port used by google map except 80 let me know.

0
 
Ludo_DirckxAuthor Commented:
Hi Bol,
do you know any web services that gives lon,lat values by address?
0
 
Ludo_DirckxAuthor Commented:
HI Bol,
I found a solution which is working exactly how i want.
The code is bellow.

Thanks for your time and help.

regards,
Antony
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
-->
    <title>
      Converting Addresses to/from Latitude/Longitude in One Step (Geocoding)
    </title>
    <script src=../email.js></script>
  </head>
  <body>
 
          <script>
        document.cookie = "test=1;expires=0";
        if (document.cookie == "") {
          document.write("Cookies must be enabled in order to use this site");
        }
      </script>
 
    <br><br><script src='http://maps.google.com/maps?file=api&v=2x&key=ABQIAAAAYFu3SS4AexmkvUfULI2FdhTNsN1UfuRzBjsbcuEM5gBRTG3onRRY4WPkPgoQqD-57teMWkem9JvZRQ' type='text/javascript'></script>
<script src='google.js'></script>
<table border='1'>
  <tr><td>from <a href='http://maps.google.com/maps?q=laarbeeklaan+21,brussel+Brussel+1090' target='_blank'>google</a></td><td>latitude</td><td>longitude</td></tr>
  <tr><td>decimal</td><td id='lat'></td><td id='lng'></td></tr>
  <tr><td>deg-min-sec</td><td id='latDMS'></td><td id='lngDMS'></td></tr>
</table>
<script>
 
var loc = 'laarbeeklaan+21, brussel, Brussel BE';
Google(loc, 'lat', 'lng', 'latDMS', 'lngDMS');
</script>
 
</table>
Laarbeeklaan, 70, 1090, Jette, B
    <script>
 
      function GetLatlon() {
        var form = document.mainform;
        if (form.city.value == "") {
          alert("City field must not be blank");
          return;
        }
        if (form.state.value == "" &&
             (form.country.value == "US" || form.country.value=="CA")) {
          alert("State field for US and Canada must not be blank");
          return;
        }
 
        var time = new Date().getTime(); // get current time 
        document.cookie = "time=" + time; // plant the cookie 
        form.time.value = time; // insert time in a hidden field 
        form.addr2latlon.value = "1";
        form.latlon2addr.value = "0";
        form.submit();
      }
 
      function GetAddress() {
        var form = document.mainform;
        if (form.latitude.value == "") {
          alert("Latitude field must not be blank");
          return;
        }
        if (form.longitude.value == "") {
          alert("Longitude field must not be blank");
          return;
        }
 
        var time = new Date().getTime(); // get current time 
        document.cookie = "time=" + time; // plant the cookie 
        form.time.value = time; // insert time in a hidden field 
        form.latlon2addr.value = "1";
        form.addr2latlon.value = "0";
        form.submit();
      }
 
    </script>
 
    </center>
 
  
  </body>
</html>

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now