首页 文章

访问地址,使用谷歌API从经度amd lattitude状态

提问于
浏览
0

我正在开发跟踪作为Web应用程序的项目 . 现在我需要从给定的经度和经度访问街道地址,州和国家 . 所以我需要代码 . 如何从经度和纬度访问地址位置?

我必须使用的Google API:https://maps.googleapis.com/maps/api/geocode/outputFormat?parameters .

我还需要谷歌反向地理编码与JSON?

1 回答

  • 0

    我这样做,(代码是自我解释):

    [...]
    GeoApiContext context = new GeoApiContext().setApiKey("your-key"); 
    GeocodingApiRequest request = GeocodingApi.newRequest(context); 
    request.latlng(new LatLng(lat, lon)); 
    request.resultType(AddressType.STREET_ADDRESS); 
    
    GeocodingResult[] results = request.await(); // try catch?
    
    for(GeocodingResult r : results){
        for (AddressComponent ac : r.addressComponents) { 
            System.out.println(ac);
        }
    }
    

    检查AddressComponentType

    for(GeocodingResult r : results){
        for (AddressComponent ac : r.addressComponents) { 
            for (AddressComponentType acType : ac.types) {
                if (acType == AddressComponentType.LOCALITY) { 
                    System.out.println(ac.longName);
                } else if (acType == AddressComponentType.STREET_ADDRESS) {
                   System.out.println(ac.longName); 
                } else if (acType == AddressComponentType.COUNTRY) {
                   System.out.println(ac.longName); 
                }
            }
        }
    }
    

    别忘了包含maven依赖:

    <dependency>
        <groupId>com.google.maps</groupId>
        <artifactId>google-maps-services</artifactId>
        <version>0.1.15</version>
    </dependency>
    

相关问题