开发者

Create single value from Latitude and Longitude pair in PHP

I'm working on a Google Maps project within a PHP application. I need to find a way to store a latitude and longitude pair as a singular value. I'm using this to help store markers with identical coordinates.

Is there way I can encode the latitude/longitude pair into a single string? I would like something like:

$lat 开发者_StackOverflow中文版= 45.18392;
$long = 62.18293;

// Would return a single string like 893jfd8sj39k
$single_string = make_single_string($lat, $long);

function make_single_string($lat, $long)
{
    // Do something here with $lat and $long
    return $single_string;
}

The most important part of the function is that it must return the same string EVERY time.

Thanks for the help!


I think you are looking for this:

function make_single_string($lat, $long)
{
    $single_string = md5( $lat.'-'.$long );
    return $single_string;
}

result will be something like this:

04f59f6c007149079d0b5cc81ee0d25f 


You can return a hash of both numbers:

function make_single_string($lat, $long)
{
    return md5($lat.'-'.$long);
}


Cant you concatenate the string on the return? with .'s...

return $lat . "," . $long;


Is there a reason that you're not using an object for this?

$lat = 45.18392;
$long = 62.18293;

$pos = array(
    'lat'=>$lat,
    'long'=>$long
);

It's much more readable and as a bonus you don't have to parse it later (can be easily serialized as well).


If you want reversibility (and you probably do) then you need to come up with a standard input format. Assuming your latitude can go between +/-90, and your longitudes between +/-180, you could do something like:

  1. Make sure the latitude and longitude have signs. If positive, prepend the + sign.
  2. Zero-pad on the front to two digits (for latitude) and three digits (for longitude). 9.2 degrees of latitude would become +09.2.
  3. Zero-pad on the end to however many significant digits you want (say, six). 9.2 degrees of latitude would then become +09.200000.
  4. Convert +, -, and . to some kind of standard notation. Say, P for +, M for -, and d for .. For 9.2 degrees of latitude you'd get P09d200000.
  5. Concatenate the latitude and longitude.

Then you get a string that's reversible and only has alphanumeric characters in it. You might not need something this fancy, though.


What you're looking for really exists, and is not as simple as md5( $lat.'-'.$long );, as to be effective must be done before a process of generalization.

It´s called Geohash. In PHP there are different implementations. This is good, but Google offers several options.


Why not just concat them?

$string = $lat . "-" . $long;

I mean if you really wanted to I supposed you could take the above and use md5 on it:

$string = md5($lat . "-" . $long);


Use a class or plain object.

class

class Coord {
   public $lat;
   public $long;

   __construct($lat, $long) {
      $this->lat = $lat;
      $this->long = $long;
   }
}

$coord = new Coord(45.18392, 62.18293);
echo $coord->lat;
echo $coord->long;

plain object

$coords = new stdClass();
$coords->lat = 45.18392;
$coords->long = 62.18293;
echo $coord->lat;
echo $coord->long;

If you needs to save these in a database, you could serialize the objects into a string, then unserialize as you pull them again from the database.

http://php.net/manual/en/function.serialize.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜