How to get latitude and longitude for a place in an isomorphic application on server

青春壹個敷衍的年華 提交于 2019-12-24 07:16:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!