A few days ago in the development of mobile phone site encountered such a demand, with a certain degree of map positioning function to see where they are now, at the same time to show themselves as the center, around 1000 meters within the business. function is very simple, get their own latitude and longitude, calculate the latitude and longitude coordinates of four points, from the database to take out the corresponding merchant, shown on the map can, the key is to need such a useful function. Fortunately found a good, share to everyone, it is estimated that we do this piece of time should also be used.
/** * calculates the four points of a square with a distance around a latitude and longitude * the radius of the Earth, with an average radius of 6371km * @param lng float longitude * @param lat float Latitude * @param distance float radius of the circle in which the point is located, the circle is tangent to this square, and the default value is 0.5 km *@ The latitude and longitude coordinates of the four points of a return array square */ function returnsquarepoint ($lng, $lat, $distance = 0.5) { $dlng = 2 * asin (sin ($distance / (2 * 6371)) / cos (Deg2rad ($lat))); $dlng = rad2deg ($DLNG); $dlat = $distance/6371; $dlat = rad2deg ($dlat); return array ( ' Left-top ' =>array (' lat ' = $lat + $dlat, ' LNG ' + $lng-$dlng), ' Right-top ' =>array (' lat ' = $lat + $dlat, ' LNG ' =>$ lng + $dlng), ' Left-bottom ' =>array (' lat ' = $lat - $dlat, ' LNG ' $lng - $dlng), ' Right-bottom ' =>array (' lat ' = $lat - $dlat, ' LNG ' + $dlng $lng) ); }
do a certain degree of development of the map will also use a very useful function, that is, through the latitude and longitude between two points, calculate the distance between two points, also share it.
/** * @desc Calculate distance * @param float $lat Latitude value * @param based on latitude and longitude between two points float $lng Longitude value */function getdistance ($lat 1, $lng 1, $lat 2, $lng 2) { $earthRadius = 6367000; $lat 1 = ($lat 1 * pi () ) / 180; $lng 1 = ($lng 1 * pi () ) / 180; $lat 2 = ($lat 2 * pi () ) / 180; $lng 2 = ($lng 2 * pi () ) / 180; $calcLongitude = $lng 2 - $lng 1; $ calclatitude = $lat 2 - $lat 1; $stepOne = pow (sin ($calcLatitude / 2), 2) + cos ($lat 1) * cos ($lat 2) * pow (sin ($calcLongitude / 2),  2); $stepTwo = 2 * asin (min (1, sqrt ($stepOne))); $calculatedDistance = $earthRadius * $stEptwo; return round ($calculatedDistance); }
Records two function--1. Calculates the four points of a square of a distance around a latitude and longitude. 2. Calculate distance according to latitude and longitude between two points