2017-05-26 71 views

回答

1

谷歌是爲他們的地圖服務提供了一個非常好的圖書館,其中包括地理代碼查找。

我爲您編寫了一個簡單的代碼片段,但請參閱地圖庫以查看您需要的API密鑰。

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"); 

    } 
} 
+0

如何創建GeocodingResult []數組? – Barrier

+0

你可以發送這個類與進口? – Barrier

+0

我添加了導入。 – Zallentine

3

U可以使用谷歌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()); 
+0

如何使用這個網址? – Barrier