Javascript 谷歌地图地理代码不工作(无标记)
我有一个网页来查找纬度、经度,并获得该位置的标记。我使用谷歌地图 我的网页从用户输入中获得2个地址,地址1和地址2,并调用Javascript 谷歌地图地理代码不工作(无标记),javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有一个网页来查找纬度、经度,并获得该位置的标记。我使用谷歌地图 我的网页从用户输入中获得2个地址,地址1和地址2,并调用codemaddress() 这是我的JavaScript代码: var映射; var address1=document.getElementById('address1')。值; var address2=document.getElementById('address2')。值; 函数初始化(){ var-latlng=new google.maps.latlng(
codemaddress()
这是我的JavaScript代码:
var映射;
var address1=document.getElementById('address1')。值;
var address2=document.getElementById('address2')。值;
函数初始化(){
var-latlng=new google.maps.latlng(-7.2759201112.791871);
变量映射选项={
缩放:12,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById('map-canvas'),mapOptions);
}
函数代码地址(){
var gc=google.maps.Geocoder();
地理编码({
“地址”:地址1
},功能(res1,状态){
if(status==google.maps.GeocoderStatus.OK){
地理编码({
“地址”:地址2
},功能(res2,状态){
if(status==google.maps.GeocoderStatus.OK){
新的google.maps.Marker({
位置:res1[0]。geometry.location,
地图:地图
});
新的google.maps.Marker({
位置:res2[0]。geometry.location,
地图:地图
});
}
});
}
});
}
当我单击按钮“查找”时,我没有得到标记。有人能帮我吗?修改
codemaddress
功能如下:
function codeAddress() {
var gc = new google.maps.Geocoder(); // notice new keyword
initialize(); // Calling initialize. If you skip it, maps aren't loading
gc.geocode({
'address': address1
}, function(res1, status) {
if (status == google.maps.GeocoderStatus.OK) {
gc.geocode({
'address': address2
}, function(res2, status) {
if (status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: res1[0].geometry.location,
map: map
});
new google.maps.Marker({
position: res2[0].geometry.location,
map: map
});
}
});
}
});
确保两个输入都有一些值来测试它
演示:- 当函数运行时,从窗体更新地址变量
function codeAddress() { var address1 = document.getElementById('address1').value; var address2 = document.getElementById('address2').value;
- 检查状态!=嗯
} else alert("Geocode failed of " + address1 + ", status=" + status);
- 在google maps地理编码器构造函数之前使用“new”
var gc = new google.maps.Geocoder();
- 在显示新标记之前删除现有标记,以便标记不会在地图上累积
if (marker1 && marker1.setMap) marker1.setMap(null);
var map = null;
var marker1 = null;
var marker2 = null;
var gc = new google.maps.Geocoder();
function initialize() {
var latlng = new google.maps.LatLng(-7.275920, 112.791871);
var mapOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
codeAddress();
}
function codeAddress() {
var address1 = document.getElementById('address1').value;
var address2 = document.getElementById('address2').value;
gc.geocode({
'address': address1
}, function (res1, status) {
if (status == google.maps.GeocoderStatus.OK) {
gc.geocode({
'address': address2
}, function (res2, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (marker1 && marker1.setMap) marker1.setMap(null);
marker1 = new google.maps.Marker({
position: res1[0].geometry.location,
map: map
});
if (marker2 && marker2.setMap) marker2.setMap(null);
marker2 = new google.maps.Marker({
position: res2[0].geometry.location,
map: map
});
} else alert("Geocode failed of " + address2 + ", status=" + status);
});
} else alert("Geocode failed of " + address1 + ", status=" + status);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
您可以指出他缺少
initialize()
和new
关键字。我认为不需要完整的代码。如果我们必须包含JSFIDLE链接,我认为我们需要提供一些代码。我们的代码是正确的。但是当我从web输入文本中设置值时,我没有得到标记,我认为这是因为最初您将输入值存储在变量中,并且假设输入的初始值为零。因此,变量不包含任何值,并将其传递给映射。如果您尝试将address1和address2的变量初始化代码移动到codeAddress中,那么它将起作用,因为您的值是动态的。如果它们是常数,那么你就可以遵循这个approach@KK是的,但是没有必要写两次代码。除非你只是想让OP复制粘贴,而不是从你的答案中学到任何东西。