How to get a time zone from a location using latitude and longitude coordinates?

后端 未结 17 1560
走了就别回头了
走了就别回头了 2020-11-21 04:38

Given the latitude and longitude of a location, how does one know what time zone is in effect in that location?

In most cases, we are looking for an IANA/Olson time z

17条回答
  •  独厮守ぢ
    2020-11-21 05:08

    If you want to use geonames.org then use this code. (But geonames.org is very slow sometimes)

    String get_time_zone_time_geonames(GeoPoint gp){
    
    
            String erg = "";
    
            double Longitude = gp.getLongitudeE6()/1E6;
            double Latitude = gp.getLatitudeE6()/1E6;
    
    
    
            String request = "http://ws.geonames.org/timezone?lat="+Latitude+"&lng="+ Longitude+ "&style=full";
    
            URL time_zone_time = null;
    
            InputStream input;
           // final StringBuilder sBuf = new StringBuilder();
    
    
            try {
                time_zone_time = new URL(request);
    
    
            try {
                input = time_zone_time.openConnection().getInputStream();
    
    
            final BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                final StringBuilder sBuf = new StringBuilder();
    
                String line = null;
                try {
                    while ((line = reader.readLine()) != null) {
                        sBuf.append(line);
                    }
                } catch (IOException e) {
                        Log.e(e.getMessage(), "XML parser, stream2string 1");
                } finally {
                    try {
                        input.close();
                    } catch (IOException e) {
                        Log.e(e.getMessage(), "XML parser, stream2string 2");
                    }
                }
    
    
    
    
                 String xmltext = sBuf.toString();
    
    
                 int startpos = xmltext.indexOf("

    And use it with:

    GeoPoint gp = new GeoPoint(39.6034810,-119.6822510);
    String Current_TimeZone_Time = get_time_zone_time_geonames(gp);
    

提交回复
热议问题