Determine timezone from latitude/longitude without

2019-01-03 23:21发布

is there any possibility to determine the timezone of point (lat/lon) without using webservices? Geonames.org is not stable enough for me to use :( I need this to work in PHP.

Thanks

16条回答
虎瘦雄心在
2楼-- · 2019-01-04 00:06

How about this ?

// ben@jp

function get_nearest_timezone($cur_lat, $cur_long, $country_code = '') {
    $timezone_ids = ($country_code) ? DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country_code)
                                    : DateTimeZone::listIdentifiers();

    if($timezone_ids && is_array($timezone_ids) && isset($timezone_ids[0])) {

        $time_zone = '';
        $tz_distance = 0;

        //only one identifier?
        if (count($timezone_ids) == 1) {
            $time_zone = $timezone_ids[0];
        } else {

            foreach($timezone_ids as $timezone_id) {
                $timezone = new DateTimeZone($timezone_id);
                $location = $timezone->getLocation();
                $tz_lat   = $location['latitude'];
                $tz_long  = $location['longitude'];

                $theta    = $cur_long - $tz_long;
                $distance = (sin(deg2rad($cur_lat)) * sin(deg2rad($tz_lat))) 
                + (cos(deg2rad($cur_lat)) * cos(deg2rad($tz_lat)) * cos(deg2rad($theta)));
                $distance = acos($distance);
                $distance = abs(rad2deg($distance));
                // echo '<br />'.$timezone_id.' '.$distance; 

                if (!$time_zone || $tz_distance > $distance) {
                    $time_zone   = $timezone_id;
                    $tz_distance = $distance;
                } 

            }
        }
        return  $time_zone;
    }
    return 'none?';
}
//timezone for one NY co-ordinate
echo get_nearest_timezone(40.772222,-74.164581) ;
// more faster and accurate if you can pass the country code 
echo get_nearest_timezone(40.772222, -74.164581, 'US') ;
查看更多
ら.Afraid
3楼-- · 2019-01-04 00:06

You can use time zone boundaries, provided here:

http://www.opensource.apple.com/source/TimeZoneData/

查看更多
萌系小妹纸
4楼-- · 2019-01-04 00:07

I use geocoder.ca

Input any location in North America, Output geocodes, area codes and timezone in json or jsonp.

For example: http://geocoder.ca/1600%20pennsylvania%20avenue,Washington,DC

Area Code: (202) Time Zone: America/New_York

Json:

{"standard":{"staddress":"Pennsylvania Ave","stnumber":"1600","prov":"DC","city":"WASHINGTON","postal":"20011","confidence":"0.8"},"longt":"-76.972948","TimeZone":"America/New_York","AreaCode":"202","latt":"38.874533"}
查看更多
太酷不给撩
5楼-- · 2019-01-04 00:08

I know this is old, but I spent some time looking for this answer. Found something very useful. Google does time zone lookups by long/lat. 2,500 per day limit (or 100,000 for business users).

https://developers.google.com/maps/documentation/timezone/

查看更多
登录 后发表回答