我正在使用指令在我的AngularJS应用中将Google地图与API集成。
示例代码:
HTML:
<boats-map location="Mallorca, Islas Baleares, España" latitude="" longitude="" zoom="8" class="map-container" style="height: 300px; margin-bottom: 20px"></boats-map>
这是我的地图初始化:
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false" defer></script>
JS:
... # I don´t put all the directive code, because I think it´s not relevant. Let me know if you need it
geocoder.geocode({'address': attrs.location}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
关键是它有时有效,有时不依赖于地址。工作示例:location =“Calle Noruega,6 Palma de Mallorca,Islas Baleares,España”
NOT工作示例:location =“Mallorca,Islas Baleares,España”状态代码= REQUEST_DENIED。
答案 0 :(得分:2)
重点是它有时有效,有时不依赖于地址。
您已经确定了地理编码服务的本质:向may return more than one result.
外部服务器发出的异步请求我在这里要说的是,我们假设问题是代码,实际上它可能不一定如此。如果您在快速间隔内连续测试了相同的输入地址,并且服务器返回始终错误,这是因为地理编码不“喜欢”模糊,不完整的查询,并且可能服务器未找到您输入的地址。即使Google在其有关位置偏差的文档中发出警告,并且在处理不完整的地址时也使用自动填充服务,这种可能性也非常小。 (这也是因为地理编码是资源密集型的,并且在服务器上放置了一些工作负载,如果要构建对延迟问题敏感的应用程序,则需要考虑这一点。如果我是你,我也不会排除网络问题,因为如上所述,有时你会收到错误,有时你却没有。
接下来,您编码:
...#我没有把所有的指令代码都放进去,因为我觉得它不相关。
为什么不在地理编码器初始化程序和if语句之间包含1或2个控制台日志?
geocoder.geocode({'address': attrs.location}, function(results, status) {
console.log(results);
console.log(results.length);
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
您应该尝试的下一个解决方案是检查您的Google密钥是否已在Google控制台中正确实施;由于不正确的实现,有时会抛出您得到的错误。
或者,您应该include a Minimal, Complete, and Verifiable example,以便我可以尝试重现您遇到的问题。