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

后端 未结 17 1556
走了就别回头了
走了就别回头了 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 04:59

    Ok here is the short Version without correct NTP Time:

    String get_xml_server_reponse(String server_url){
    
    URL xml_server = null;
    
    String xmltext = "";
    
    InputStream input;
    
    
    try {
        xml_server = new URL(server_url);
    
    
        try {
            input = xml_server.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");
                }
            }
    
            xmltext =  sBuf.toString();
    
        } catch (IOException e1) {
    
                e1.printStackTrace();
            }
    
    
        } catch (MalformedURLException e1) {
    
          e1.printStackTrace();
        }
    
     return  xmltext;
    
    } 
    
    
    long get_time_zone_time_l(GeoPoint gp){
    
    
            String raw_offset = "";
            String dst_offset = "";
    
            double Longitude = gp.getLongitudeE6()/1E6;
            double Latitude = gp.getLatitudeE6()/1E6;
    
            long tsLong = System.currentTimeMillis()/1000;
    
    
            if (tsLong != 0)
            {
    
            // https://maps.googleapis.com/maps/api/timezone/xml?location=39.6034810,-119.6822510×tamp=1331161200&sensor=false
    
            String request = "https://maps.googleapis.com/maps/api/timezone/xml?location="+Latitude+","+ Longitude+ "×tamp="+tsLong +"&sensor=false";
    
            String xmltext = get_xml_server_reponse(request);
    
            if(xmltext.compareTo("")!= 0)
            {
    
             int startpos = xmltext.indexOf("

    And use it with:

    GeoPoint gp = new GeoPoint(39.6034810,-119.6822510);
    long Current_TimeZone_Time_l = get_time_zone_time_l(gp);
    

提交回复
热议问题