问题
I have isomorphic app which loads on server and client as well. My concern is about loading results for the application on server I need to call google maps api service to get lattitude and longitude for the place for ex 'www.mysite.com/search/New-York--NY--United-States' then I need to find its latitude and longitude from google maps api with below code in php(my api is in php)
$address= $this->input->get('address');
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');
$geo = json_decode($geo, true);
if ($geo['status'] = 'OK') {
$lat = $geo['results'][0]['geometry']['location']['lat'];
$lng = $geo['results'][0]['geometry']['location']['lng'];
}
Then based on this latitude and longitude I search for my results as below
$radius = 6371;
$distance = 250;
// latitude boundaries
$maxlat = $lat + rad2deg($distance / $radius);
$minlat = $lat - rad2deg($distance / $radius);
// longitude boundaries (longitude gets smaller when latitude increases)
$maxlng = $lng + rad2deg($distance / $radius / cos(deg2rad($lat)));
$minlng = $lng - rad2deg($distance / $radius / cos(deg2rad($lat)));
With above code I calculate maximum and minimum lattitude and longitude and then look for my results by applying where conditions as below.
$this->db->where('rv.LATITUDE >=', $minlat);
$this->db->where('rv.LATITUDE <=', $maxlat);
$this->db->where('rv.LONGTITUDE >=', $minlng);
$this->db->where('rv.LONGTITUDE <=', $maxlng);
How to get latitude and longitude without calling google maps api and how to optimize it to get results faster. Please provide suggestions regarding this question.
回答1:
If you're not trying to pin down street addresses and only interested in discrete place names like cities, landmarks, etc (in your example, New York, USA) you might look into the various GeoNames exportables. You could load them into a local datastore and perform lookups by name.
来源:https://stackoverflow.com/questions/42235025/how-to-get-latitude-and-longitude-for-a-place-in-an-isomorphic-application-on-se