Link to home
Start Free TrialLog in
Avatar of GaryHoather
GaryHoather

asked on

How do I obtain the longitude and latitude of the current location using the Mappoint 2010 Map Control

I have been asked to complete an application on behalf of a programmer who has left the Company.  The application is a fleet management system and ises Mappoint 2010 ( Europe ) for location and mapping.

I can find a location using the mappoint [FindPlaceResults] procedure but inorder to identify the nearest current ehicle I need to query the sql table which uses lat/longs to keep track of vehicle location.

Idealliy I would like to be able to find the Lat/Long of the current location returned by using the [FindPlaceResults] procedure.  I am only taking the top entry from the procedure so there will only be ever one location returned.  As the map centres on the location found I had hoped the the lat/long could be obatined.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In semi-related news, here is my teaching example of how to call the major geocoder APIs.  Give them an address and they can give you back the Lat/Lon pair in a decimal number, suitable for use in the Haversine formula.  Google has recently revised and extended its geocoder to make it perform reverse geo-coding.  You can give it a Lat/Lon pair and it will give you the nearest address.  How cool is that!

Please let me know if you have further questions or if I can help with anything else about the theory and practice of proximity calculation.  As I said, I do not know Mappoint, but the principles and geometry are going to be the same no matter what software you choose.  Best, ~Ray
<?php // RAY_class_SimpleGeoCoder.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;


// API KEYS, ETC., IF REQUIRED
// require_once('local_data.php');


// A FREEFORM NAMED LOCATION STATEMENT IS OPTIONAL - PROCESSED FIRST
if (!empty($_GET["n"]))
{
    $location = trim($_GET["n"]);
}
// ADDRESS, CITY, STATE, ZIP ARE OPTIONAL - PROCESSED IF FREEFORM LOCATION IS NOT PRESENT
else
{
    $location = '';
    if (!empty($_GET["a"])) { $location .= $_GET["a"] . ' '; }
    if (!empty($_GET["c"])) { $location .= $_GET["c"] . ' '; }
    if (!empty($_GET["s"])) { $location .= $_GET["s"] . ' '; }
    if (!empty($_GET["z"])) { $location .= $_GET["z"] . ' '; }
    $location = trim($location);
}

// IF WE HAVE A LOCATION STRING, RUN THE GEOCODERS
if ($location)
{
    // PREPARE THE SIMPLE GEO-CODER
    $demo = new SGC;

    // TEST THE YAHOO! GEOCODER
    $demo->YGC($location);
    echo PHP_EOL . "YAHOO! ";
    print_rr($demo);

    // TEST THE GOOGLE GEOCODER
    $demo->GGC($location);
    echo PHP_EOL . "GOOGLE ";
    print_rr($demo);
}
// ALL DONE PROCESSING THE INPUT, PUT UP THE FORM
?>
<html>
<head>
<title>Yahoo/Google SimpleGeoCoder Demo</title>
</head>
<body>
<form method="get">
Try using a full or partial address.
Addr: <input type="text" name="a" autocomplete="off" />
City: <input type="text" name="c" autocomplete="off" />
ST:   <input type="text" name="s" autocomplete="off" size="2" />
Zip:  <input type="text" name="z" autocomplete="off" size="8" />
<input type="submit" value="GeoCode This Address" />

Or use the name of a location.
Name: <input type="text" name="n" autocomplete="off" />
<input type="submit" value="GeoCode This Location Name" />
</form>
</body>
</html>

<?php
// SIMPLE GEOCODER CLASS
class SGC
{
    // DECLARE THE WORKING DATA
    private $precis;

    // DECLARE THE OUTPUT DATA
    public $latitude;
    public $longitude;
    public $precision;
    public $warning;
    public $error;
    public $geocoder;

    // DECLARE THE CONSTRUCTOR
    public function __construct()
    {
        $this->latitude  = 0.0;
        $this->longitude = 0.0;
        $this->precision = FALSE;  // WANT A VALUE OF 5 OR HIGHER, HIGHER IS BETTER, 8 IS ON THE ROOFTOP
        $this->warning   = '';
        $this->geocoder  = '';
        $this->error     = '';
        unset($this->precis);
    }

    // DECLARE THE DATA-CLEANUP
    private function _cleanup($str)
    {
        $str = preg_replace('/[^\' a-zA-Z0-9&!#$%()"+:?\/@,_\.\-]/', '', $str);
        return trim(preg_replace('/\s\s+/', ' ', $str));
    }

    // DECLARE THE YAHOO! VERSION OF THE WORKHORSE
    public function YGC($location)
    {
        $loc = $this->_cleanup($location);
        if (empty($loc))
        {
            $this->error = "LOCATION DATA IS EMPTY";
            return FALSE;
        }
        if (!defined('YAHOO_API')) define('YAHOO_API', 'YAHOO_API');
        $this->geocoder = 'Yahoo!';
        $yahooUrl = "http://local.yahooapis.com/MapsService/V1/geocode?&appid=" . YAHOO_API;
        $yahooUrl .= "&location=" . urlencode($loc);

        // EXECUTE YAHOO GEOCODER QUERY
        // NOTE - USE ERROR SUPPRESSION OR IT WILL BARK OUT THE YAHOO API KEY - ON FAILURE RETURNS HTTP 400 BAD REQUEST
        if ($yfp = @fopen($yahooUrl, 'r'))
        {
            $yahooResponse = '';
            while (!feof($yfp))
            {
                $yahooResponse .= fgets($yfp);
            }
            fclose($yfp);
        }
        // IF SOMETHING IS SICK AT YAHOO
        else
        {
            $this->error = "UNABLE TO OPEN $yahooUrl";
            return FALSE;
        }

        // EXAMINE THE RESULT
        if ($yahooResponse != '') // NOT EMPTY, WE GOT DATA
        {
            $ydata = new SimpleXMLElement($yahooResponse);

            // CHECK FOR ANY ERROR MESSAGE, IF NONE, EXTRACT THE DATA POINTS
            $yerror = $ydata->Message;
            if ($yerror == '')
            {
                $this->precis    = (string)$ydata->Result["precision"];
                $this->warning   = (string)$ydata->Result["warning"];
                $this->latitude  = (string)$ydata->Result->Latitude;
                $this->longitude = (string)$ydata->Result->Longitude;

                // THESE STATEMENTS CAN BE USED TO RETURN NORMALIZED ADDRESS
                $this->address   = (string)$ydata->Result->Address;
                $this->city      = (string)$ydata->Result->City;
                $this->state     = (string)$ydata->Result->State;
                $this->zip       = (string)$ydata->Result->Zip;

                // SET PRECISION TO A NUMBER VALUE
                if ($this->precis == 'zip')     { $this->precision = "5"; }
                if ($this->precis == 'street')  { $this->precision = "6"; }
                if ($this->precis == 'address') { $this->precision = "8"; }
            }
            else
            {
                $this->error = "ERROR: $yahooUrl SAYS $yerror";
                return FALSE;
            }
        }

        // NO RESULT - SOMETHING IS SICK AT YAHOO
        else
        {
            $this->error = "NO DATA RETURNED FROM $yahooUrl";
            return FALSE;
        }
        return TRUE;
    } // END function geocodeYahoo


    // DECLARE THE GOOGLE VERSION OF THE WORKHORSE
    public function GGC($location)
    {
        $loc = $this->_cleanup($location);
        if (empty($loc))
        {
            $this->error = "LOCATION DATA IS EMPTY";
            return FALSE;
        }

        if (!defined('GOOGLE_API')) define('GOOGLE_API', 'GOOGLE_API');
        $this->geocoder = 'Google';
        $googleUrl = "http://maps.google.com/maps/geo?key=" . GOOGLE_API . "&output=csv";
        $googleUrl .= "&q=" . urlencode($loc);

        // EXECUTE GOOGLE GEOCODER QUERY
        if ($gfp = @fopen($googleUrl, 'r'))
        {
            $googleResponse = '';
            while (!feof($gfp))
            {
                $googleResponse .= fgets($gfp);
            }
            fclose($gfp);
        }
        else
        {
            $this->error = "UNABLE TO OPEN $googleUrl";
            return FALSE;
        }

        // EXTRACT THE DATA FROM THE CSV STRING
        $gdata = explode(',',$googleResponse);
        if ($gdata[0] != '200') // RESPONSE CODE SHOULD BE '200' -- IF 602 - BAD ZIP CODE OR UNUSABLE ADDRESS
        {
            $this->error = "ERROR CODE {$gdata[0]} FROM $googleUrl";
            return FALSE;
        }
        $this->precision = $gdata[1]; // GEOCODE ACCURACY - ZIP CODE = 5, HIGHER NUMBERS ARE BETTER
        $this->latitude  = $gdata[2];
        $this->longitude = $gdata[3];
        return TRUE;
    } // END function geocodeGoogle

} // END class SimpleGeocoder




// UNRELATED FUNCTION TO MAKE THE OUTPUT SHOW THE PUBLIC INFORMATION ONLY
function print_rr($thing)
{
    $str = print_r($thing, TRUE);
    $arr = explode(PHP_EOL, $str);
    foreach ($arr as $ptr => $txt)
    {
        if (preg_match('/:private]/', $txt))
        {
            unset($arr[$ptr]);
        }
    }
    echo implode(PHP_EOL, $arr);
}

Open in new window