openlayers 和谷歌地理编码器
我正在尝试将谷歌地理编码服务与 openlayer 地图一起使用。这应该可以毫无问题地工作......不是吗? 我使用的代码就像 googles geocode api doc 上的示例一样:
function geoCode(){
var adresse = $("observation_location_text").getValue();
var geoCoder = new google.maps.Geocoder();
alert(adresse);
geoCoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker.moveTo(results[0].geometry.location);
marker.display(true);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
}); }
由于某种原因,我没有获得 Geocoder 对象。带有地址的警报永远不会被执行。
期待一些提示 一月
i'm trying to use the google geocoding service with an openlayer map. this shoudl work without problem...should it not?
the code i'm using is just like the example on googles geocode api doc:
function geoCode(){
var adresse = $("observation_location_text").getValue();
var geoCoder = new google.maps.Geocoder();
alert(adresse);
geoCoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker.moveTo(results[0].geometry.location);
marker.display(true);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
}); }
for some reason i do not get the Geocoder object. the alert with address is never executed.
looking forward to some hints
jan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试从 OpenLayers 中删除任何依赖项并查看它是否有效。您应该能够使用普通的 Google Maps API 创建地理编码器对象。
我注意到的一件小事是,您从输入中读取值到名为“adresse”的变量中,但传递给名为“address”的地理编码器变量。如果这只是一个拼写错误并且未声明第二个变量,您会收到脚本错误并且代码停止执行。也许这就是为什么你的地理编码器对象永远不会被创建的原因。
Try to remove any dependency from OpenLayers and see if it works. You should be able to create geocoder object with plain Google Maps API.
On little thing I noticed is that you read value from input into variable called "adresse" but pass to geocoder variable callled "address". If it's just a typo and second variable is not declared you get script error and code stops executing. Probably that's why you geocoder object is never created.
阅读第一个答案的评论 - 您确定运行“new google.maps.Geocoder()”时 Google 地图文件已加载吗?
您说代码在加载时被调用,您以什么方式执行它?由于您使用的是 jQuery,因此应该在 $(document).ready() 中完成。
Reading the comments on the first answer - are you sure the Google Maps files have loaded when you run "new google.maps.Geocoder()"?
You say that the code gets called on load, in what way do you do it? Since you are using jQuery, it should be done in $(document).ready().
我正在尝试做和你完全相同的事情。这是我的问题,它可能会帮助您链接
另外,如果您尝试在 openlayers 图层上传递地理编码点,请注意 openlayers 矢量图层上的点类似于 (lon, lat),但来自 Google 地理编码的点类似于 (lat,lon)。我从 Google 页面复制:“位置包含地理编码的纬度、经度值。请注意,我们将此位置作为 LatLng 对象返回,而不是作为格式化字符串”( Google 页面)
但我认为问题在于“不幸的是,谷歌地理编码器规定您使用坐标来映射到谷歌地图上。” ,正如 El Duderino 所说...
哦,还有,如果您使用 Drupal,请检查 这个出来了...
i' m trying to do the exact same thing as you. Here is my problem, it may help you link
Also , if you try to pass the geocoding's point on an openlayers' layer be aware that the points on openlayers vector layers are like (lon, lat) BUT the points came from Google geocoding are like (lat,lon). I'm copying from Googles page : "location contains the geocoded latitude,longitude value. Note that we return this location as a LatLng object, not as a formatted string" ( Googles page)
But I think the problem is that "google geocoder unfortunately dictate that you use the coordinates to map onto a google map.", as El Duderino says...
Oh, and also, if you use Drupal, check this out...