Google maps api 3 如何在传单JS中使用来自Google Geocoder的视口?
我已经成功地建立了一个传单JS地图,它使用谷歌地图地理编码器平移到一个地理编码的地址。但我也尝试使用“map.fitBounds”从视口中获取适当的缩放级别,但它似乎不起作用。我使用的代码是:Google maps api 3 如何在传单JS中使用来自Google Geocoder的视口?,google-maps-api-3,geocoding,leaflet,Google Maps Api 3,Geocoding,Leaflet,我已经成功地建立了一个传单JS地图,它使用谷歌地图地理编码器平移到一个地理编码的地址。但我也尝试使用“map.fitBounds”从视口中获取适当的缩放级别,但它似乎不起作用。我使用的代码是: map.panTo([results[0].geometry.location.lat(),results[0].geometry.location.lng()]); map.fitBounds([[results[0].geometry.viewport.southwest.lat(), resu
map.panTo([results[0].geometry.location.lat(),results[0].geometry.location.lng()]);
map.fitBounds([[results[0].geometry.viewport.southwest.lat(), results[0].geometry.viewport.southwest.lng()],[results[0].geometry.viewport.northeast.lat(), results[0].geometry.viewport.northeast.lng()]]);
请参见此处的示例:
如何修复它,使其缩放到google maps geocoder结果返回的相应视口?DrMolle指出,传单地图对象不是google maps API v3对象(在您提到的另一个问题中)。这项工作:
map.panTo([results[0].geometry.location.lat(),results[0].geometry.location.lng()]);
map.fitBounds([[results[0].geometry.viewport.getNorthEast()],
[results[0].geometry.viewport.getSouthWest()]]);
如以下文件所述:
- 结果[].geometry.location是一个参数,panTo需要它作为参数李>
- 结果[].geometry.viewport是一个参数,fitBounds需要它作为参数李>
- DrMolle指出,传单地图对象不是Google Maps API v3对象(在您提到的另一个问题中)。这项工作:
如以下文件所述:map.panTo([results[0].geometry.location.lat(),results[0].geometry.location.lng()]); map.fitBounds([[results[0].geometry.viewport.getNorthEast()], [results[0].geometry.viewport.getSouthWest()]]);
- 结果[].geometry.location是一个参数,panTo需要它作为参数李>
- 结果[].geometry.viewport是一个参数,fitBounds需要它作为参数李>
结果[0].geometry.viewport.southern/northern的属性(我猜您正在控制台内观察网络流量,但您在控制台中看到的不会直接传递给回调函数)。要获取西南/东北方向,请使用
的google.maps.LatLngBounds
和getsoutwest()
方法getsoutwest()
是一个google.maps.LatLngBounds-object,但不能直接在传单中使用 必须将其“转换”为数组或传单。LatLngBounds-object 这应该起作用:结果[0].geometry.viewport
map.fitBounds([ [results[0].geometry.viewport.getSouthWest().lat(), results[0].geometry.viewport.getSouthWest().lng()], [results[0].geometry.viewport.getNorthEast().lat(), results[0].geometry.viewport.getNorthEast().lng()] ]);
没有类似于
(我猜您正在控制台内观察网络流量,但您看到的不会直接传递给回调函数)。要获取西南/东北方向,请使用results[0].geometry.viewport.southern/northwest
的google.maps.LatLngBounds
和getsoutwest()
方法getsoutwest()
是一个google.maps.LatLngBounds-object,但不能直接在传单中使用 必须将其“转换”为数组或传单。LatLngBounds-object 这应该起作用:结果[0].geometry.viewport
map.fitBounds([ [results[0].geometry.viewport.getSouthWest().lat(), results[0].geometry.viewport.getSouthWest().lng()], [results[0].geometry.viewport.getNorthEast().lat(), results[0].geometry.viewport.getNorthEast().lng()] ]);
感谢您发布工作示例。这很有帮助。它并没有立即为我工作,但是,必须做一些调整,它的工作
我只是想,如果其他人不知道如何让它工作,我会把它贴出来function codeAddress() { var address = document.getElementById('address').value; geocoder.geocode( {'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[0].geometry.viewport) { map.fitBounds([[results[0].geometry.viewport.getSouthWest().lat(), results[0].geometry.viewport.getSouthWest().lng()], [results[0].geometry.viewport.getNorthEast().lat(), results[0].geometry.viewport.getNorthEast().lng()]]); map.setZoom(18); } else if (results[0].geometry.bounds) { map.fitBounds([[results[0].geometry.bounds.getSouthWest().lat(), results[0].geometry.bounds.getSouthWest().lng()], [results[0].geometry.bounds.getNorthEast().lat(), results[0].geometry.bounds.getNorthEast().lng()]]); } else { // give up, pick an arbitrary zoom level map.panTo(results[0].geometry.location); map.setZoom(15); } } else { $('#result').html('Geocode was not successful for the following reason: ' + status); } }); }
function codeAddress() { var address = document.getElementById('address').value; geocoder.geocode( {'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[0].geometry.viewport) { map.fitBounds([[results[0].geometry.viewport.getSouthWest().lat(), results[0].geometry.viewport.getSouthWest().lng()], [results[0].geometry.viewport.getNorthEast().lat(), results[0].geometry.viewport.getNorthEast().lng()]]); map.setZoom(18); } else if (results[0].geometry.bounds) { map.fitBounds([[results[0].geometry.bounds.getSouthWest().lat(), results[0].geometry.bounds.getSouthWest().lng()], [results[0].geometry.bounds.getNorthEast().lat(), results[0].geometry.bounds.getNorthEast().lng()]]); } else { // give up, pick an arbitrary zoom level map.panTo(results[0].geometry.location); map.setZoom(15); } } else { $('#result').html('Geocode was not successful for the following reason: ' + status); } }); }
再次感谢 感谢您发布工作示例。这很有帮助。它并没有立即为我工作,但是,必须做一些调整,它的工作
我只是想,如果其他人不知道如何让它工作,我会把它贴出来function codeAddress() { var address = document.getElementById('address').value; geocoder.geocode( {'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[0].geometry.viewport) { map.fitBounds([[results[0].geometry.viewport.getSouthWest().lat(), results[0].geometry.viewport.getSouthWest().lng()], [results[0].geometry.viewport.getNorthEast().lat(), results[0].geometry.viewport.getNorthEast().lng()]]); map.setZoom(18); } else if (results[0].geometry.bounds) { map.fitBounds([[results[0].geometry.bounds.getSouthWest().lat(), results[0].geometry.bounds.getSouthWest().lng()], [results[0].geometry.bounds.getNorthEast().lat(), results[0].geometry.bounds.getNorthEast().lng()]]); } else { // give up, pick an arbitrary zoom level map.panTo(results[0].geometry.location); map.setZoom(15); } } else { $('#result').html('Geocode was not successful for the following reason: ' + status); } }); }
function codeAddress() { var address = document.getElementById('address').value; geocoder.geocode( {'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[0].geometry.viewport) { map.fitBounds([[results[0].geometry.viewport.getSouthWest().lat(), results[0].geometry.viewport.getSouthWest().lng()], [results[0].geometry.viewport.getNorthEast().lat(), results[0].geometry.viewport.getNorthEast().lng()]]); map.setZoom(18); } else if (results[0].geometry.bounds) { map.fitBounds([[results[0].geometry.bounds.getSouthWest().lat(), results[0].geometry.bounds.getSouthWest().lng()], [results[0].geometry.bounds.getNorthEast().lat(), results[0].geometry.bounds.getNorthEast().lng()]]); } else { // give up, pick an arbitrary zoom level map.panTo(results[0].geometry.location); map.setZoom(15); } } else { $('#result').html('Geocode was not successful for the following reason: ' + status); } }); }
再次感谢 嗨,谢谢你的回答。我的例子中的panTo没有错误(我从这个问题的答案中复制了它),但是,很正确,fitBounds行的控制台中有一个错误——“uncaughttypeerror:cannotcallmethod'lat'of undefined”。不幸的是,您的建议似乎也不起作用,我在控制台中收到了相同的错误。您的链接对我不起作用(它给出:网络错误(tcp\U错误),出现了通信错误:“操作超时”Web服务器可能已关闭、太忙或遇到其他问题,无法响应请求。您可能希望稍后再试)。可以想象,您正在使用的特定结果不包含建议的视口,请参阅我的更新答案。感谢更新的建议。它仍然无法工作(我得到了相同的控制台错误),我猜是因为传单和谷歌API使用panTo和fitBounds的方式不同?DrMolle指出传单地图对象不是谷歌地图API v3对象(在您提到的另一个问题中)。请参阅更新的答案和工作示例。示例仍然不起作用,传单不接受
-objects,由google.maps.LatLng
和getsoutwest()
返回。您好,感谢您的回答。我的例子中的panTo没有错误(我从这个问题的答案中复制了它),但是,很正确,fitBounds行的控制台中有一个错误——“uncaughttypeerror:cannotcallmethod'lat'of undefined”。不幸的是,您的建议似乎也不起作用,我在控制台中收到了相同的错误。您的链接对我不起作用(它给出:网络错误(tcp\U错误),出现了通信错误:“操作超时”Web服务器可能已关闭、太忙或遇到其他问题,无法响应请求。您可能希望稍后再试)。可以想象,您正在使用的特定结果不包含建议的视口,请参阅我的更新答案。感谢更新的建议。它仍然无法工作(我得到了相同的控制台错误),我猜是因为传单和谷歌API使用panTo和fitBounds的方式不同?DrMolle指出传单地图对象不是谷歌地图API v3对象(在您提到的另一个问题中)。查看更新的答案和工作示例。示例仍然不起作用,传单不接受getsoutwest()
-objects,由google.maps.LatLng
和getsoutwest()
getsoutwest()返回