微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

GeocoderRequest.geocode 返回 REQUEST_DENIED

如何解决GeocoderRequest.geocode 返回 REQUEST_DENIED

我们正在使用以下代码片段来检索地址坐标,目前正在获取 REQUEST_DENIED。我在这里缺少什么?我们还需要为此使用 API 密钥吗?

import com.google.code.geocoder.Geocoder;
import com.google.code.geocoder.GeocoderRequestBuilder;
import com.google.code.geocoder.model.GeocodeResponse;
import com.google.code.geocoder.model.GeocoderRequest;
import com.google.code.geocoder.model.GeocoderResult;

公共类 GisUtil {

public static GisLocationDTO getAddressgisLocation(String addrPresentation) {
    final Geocoder geocoder = new Geocoder();
    GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(addrPresentation).setLanguage("en").getGeocoderRequest();
    GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);   
    if(geocoderResponse == null || geocoderResponse.getResults() == null) {
        return null;
    }
    List<GeocoderResult> geoResults = geocoderResponse.getResults();
    if(geoResults == null || geoResults.isEmpty()) {
        return null;
    }
    GeocoderResult geoRes = geoResults.iterator().next();
    GisLocationDTO gisLocationDTO = new GisLocationDTO();
    gisLocationDTO.setGisLatitude(geoRes.getGeometry().getLocation().getLat().toString());
    gisLocationDTO.setGisLongitude(geoRes.getGeometry().getLocation().getLng().toString());
    
    //geoRes.getGeometry().getLocation().getLng();
    return gisLocationDTO;
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。