【问题标题】:Android:How to get time or timezone according to current location(geolocation/geocode)Android:如何根据当前位置(地理位置/地理代码)获取时间或时区
【发布时间】:2017-05-26 07:37:10
【问题描述】:

我有一个地理代码(纬度,经度)。我的要求是根据这个地理代码获取时间或获取时区,无论安卓设备的时间是否改变。

对此要求有任何想法...

【问题讨论】:

    标签: java android time geocode


    【解决方案1】:

    你可以通过传递纬度和经度使用google api获取时区

    https://maps.googleapis.com/maps/api/timezone/json?location=38.908133,-77.047119&timestamp=1458000000&key=YOUR_API_KEY
    

    或者第二种方法是

    Calendar calender = Calendar.getInstance();
    TimeZone timeZone = calender.getTimeZone();
    Log.d("Time zone","="+timeZone.getDisplayName());
    

    【讨论】:

    • 我怎样才能使用这个网址?
    【解决方案2】:

    Google 为其地图服务提供了一个非常棒的库,其中包括地理代码查找。

    我为你写了一个简单的代码sn-p,但是请参考地图库看看你需要哪些API Keys。

    https://github.com/googlemaps/google-maps-services-java

    import com.google.maps.GeoApiContext;
    import com.google.maps.GeocodingApi;
    import com.google.maps.PlacesApi;
    import com.google.maps.model.GeocodingResult;
    import com.google.maps.model.PlaceDetails;
    
    public class Maps {
    
    private static String apiKey;
    
    public Maps(String apikey) {
        this.apiKey = apikey;
    }
    
    public String getTimezone(String address) throws Exception {
    
    // The API will save the most matching result of your defined address in an array
    GeocodingResult[] results = GeocodingApi.geocode(context, address).await();
    
     // .geometry.location returns an LatLng object coressponding to your address; 
    //getTimeZone returns the timezone and it will be saved as a TimeZone object 
        TimeZone timeZone = TimeZoneApi.getTimeZone(context,results[0].geometry.location).await();
    
    // returns the displayname of the timezone
        return timeZone.getDisplayName();
    
        }
    
    }
    
    public class Run {
    public static void main(String[] args) throws Exception {
    
        String apiKey = "YOURGOOGLEAPIKEY"
    
        Maps m = new Maps(apiKey);
        m.getTimezone("Amsterdam");
    
        }
    }
    

    【讨论】:

    • 如何创建 GeocodingResult[] 数组?
    • 你能把这个和类导入一起发送吗?
    • 我添加了导入。
    猜你喜欢
    • 2019-12-15
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多