Showing posts with label latitude. Show all posts
Showing posts with label latitude. Show all posts

Sunday, August 12, 2012

How to get Longitude / Latitude in PHP using Google Maps API

This post is just a simple script I did, built in PHP that will get the coordinates of specific location.

The script uses google maps web service that returns XML data in which we need parse to get the information we need.

Take note also of the URL I used, you just need to add your google maps key.
http://maps.google.com/maps/geo?output=xml&oe=utf-8&key=enter+your+key+here&q=enter+your+query+here

<?php

$key = 'AIzaSyCrPl5vXuNjPU1fgHF69YPxEopT_NziA4o'; // your google maps key
$param = 'T2G 0S7'; // the location you want to look for, this can be postal code, map id, address, etc.

$geoinfo = get_longlat($key, $param);
var_dump($geoinfo);

function get_longlat($key='', $param='') {
        $request_url = "http://maps.google.com/maps/geo?output=xml&key=$key&oe=utf-8&q=".urlencode($param);
        $xml = simplexml_load_file($request_url);

        $geoinfo = array();
        if (!empty($xml->Response)) {
                $point = $xml->Response->Placemark->Point;
                if (!empty($point)) {
                        $coordinates = explode(",", $point->coordinates);
                        $geoinfo = array(
                                'lon' => $coordinates[0],
                                'lat' => $coordinates[1]
                        );
                }
        }

        return $geoinfo;
}

?>

You can also get other information by parsing the XML returned by the google maps web service. Please see below for the sample XML return.

<kml>
<Response>
    <name>t2g 0s7</name>
    <Status>
        <code>200</code>
        <request>geocode</request>
    </Status>
    <Placemark id="p1">
        <address>Calgary, AB T2G 0S7, Canada</address>
        <AddressDetails Accuracy="5">
            <Country>
                <CountryNameCode>CA</CountryNameCode>
                <CountryName>Canada</CountryName>
                <AdministrativeArea>
                    <AdministrativeAreaName>AB</AdministrativeAreaName>
                    <Locality>
                        <LocalityName>Calgary</LocalityName>
                        <PostalCode>
                            <PostalCodeNumber>T2G 0S7</PostalCodeNumber>
                        </PostalCode>
                    </Locality>
                </AdministrativeArea>
            </Country>
        </AddressDetails>
        <ExtendedData>
            <LatLonBox north="51.0439997" south="51.0413017" east="-114.0367519" west="-114.0394858"/>
        </ExtendedData>
        <Point>
            <coordinates>-114.0380127,51.0426718,0</coordinates>
        </Point>
    </Placemark>
</Response>
</kml>

Wednesday, November 23, 2011

Computing the distance between zip codes longitude / latitude

I have created a function that will compute for the distance between zip codes (longitude / latitude).

The function will actually go a series of conversion from degrees to radians, radians to degrees, degrees to miles, then miles to any unit of measurement you want.

For the benefit of this post, we will have the distance in kilometers.

Given that, we need the multipliers below.

my $pi = atan2(1,1) * 4; # pi: 3.14159265358979
my $deg2radmul = $pi / 180; # degrees to radians multiplier: 0.0174532925199433
my $rad2degmul = 180 / $pi; # radians to degrees multiplier: 57.2957795130823
my $deg2milmul = 69.09; # degrees to miles multiplier
my $mil2kilmul = 1.609344; # miles to kilometers multiplier


First, we have to get the distance between longitude1 and longitude2 by subtracting the two points.

my $dlon = $lon1 - $lon2;


Next, get the distance by getting the radians value of each points and use sin and cos function in perl.

my $dist = sin($lat1 * $deg2radmul) * sin($lat2 * $deg2radmul) + cos($lat1 * $deg2radmul) * cos($lat2 * $deg2radmul) * cos($dlon * $deg2radmul);


Then finally, convert the distance in radians, radians to degrees, degrees to miles, and miles to kilometers.

$dist = atan2(sqrt(1 - $dist**2), $dist);
$dist = $dist * $rad2degmul; # radians to degrees
$dist = $dist * $deg2milmul; # degrees to miles
$dist = $dist * $mil2kilmul; # miles to kilometers


To get the complete implementation and example that I created, please see below. Happy Coding!

#!/usr/bin/perl

my $lat1 = '14.559943';
my $lon1 = '121.015198';
my $lat2 = '14.56255';
my $lon2 = '121.017151';

my $dist = &distance($lat1, $lon1, $lat2, $lon2);
print "distance in km: $dist\n";

#!/usr/bin/perl

my $lat1 = '14.559943';
my $lon1 = '121.015198';
my $lat2 = '14.56255';
my $lon2 = '121.017151';

my $dist = &distance($lat1, $lon1, $lat2, $lon2);
print "distance in km: $dist\n";

sub distance
{
  my ($lat1, $lon1, $lat2, $lon2) = @_;
  my ($pi, $deg2radmul, $rad2degmul, $deg2milmul, $mil2kilmul, $dlon, $dist);

  $pi = atan2(1,1) * 4; # pi: 3.14159265358979
  $deg2radmul = $pi / 180; # degrees to radians multiplier: 0.0174532925199433
  $rad2degmul = 180 / $pi; # radians to degrees multiplier: 57.2957795130823
  $deg2milmul = 69.09; # degrees to miles multiplier
  $mil2kilmul = 1.609344; # miles to kilometers multiplier

  $dlon = $lon1 - $lon2;
  $dist = sin($lat1 * $deg2radmul) * sin($lat2 * $deg2radmul) + cos($lat1 * $deg2radmul) * cos($lat2 * $deg2radmul) * cos($dlon * $deg2radmul);

  $dist = atan2(sqrt(1 - $dist**2), $dist);
  $dist = $dist * $rad2degmul; # radians to degrees
  $dist = $dist * $deg2milmul; # degrees to miles
  $dist = $dist * $mil2kilmul; # miles to kilometers

  return ($dist);
}




1;


Some men succeed by what they know; some by what they do; and a few by what they are.

Monday, March 21, 2011

How to get latitude and longtitude values from google maps

There are 2 ways on how to get the latitude and longtitude coordinates from google maps.

First option is to get it using javascript:

Just follow the simple steps below to do it.

1. Go to google maps website - http://maps.google.com

2. Click to the center of the site you want.

3. Once you are at the center, copy and paste the code below on your browser's address bar.

javascript:void(prompt('',gApplication.getMap().getCenter()));

4. A pop-up message will prompt with the latitude and longtitude values enclosing with parenthesis.

5. Check the returned values by putting it to google maps search bar and enter. An arrow will point to the map where the location is.

Problem here is that you have to be at the center of the Map to get the actual coordinates.

Resource: http://lifehacker.com


Second is to get it the easier way by enabling the options on your google maps account:

Just follow the simple steps below to activate.

1. Go to google maps website - http://maps.google.com/

2. Log-in to your google account.

3. On the upper right corner of the page, you will see the Options Icon tab.

4. Click the Options Icon tab and select "Maps Labs".

5. A lightbox page will pop-up displaying all the options available for google maps.

6. Set the "LatLng Tooltip" and "LatLng Marker" to ENABLE, then save changes.

7. Go to the map, zoom in until you get to the site you want, then click and hold the "Shift" key on your keyboard.

8. You will noticed that every time you move the cursor while holding shift button, latitude and longtitude changes accordingly.


Hope you like it!! Yeah men!!

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.