本文介绍了调用javascript函数时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
我正在使用地址解析API3,并且代码运行正常,但是问题是当我通过下拉更改事件调用codeAddress函数时.它会增加地图中的标记.我正在codeAddress中创建一个标记,该标记在每次调用codeAddress函数时都会增加.我希望在我的codeAddress函数的每次调用中都显示一个标记.
这是我的代码:
Hello to all!
I am using geocode API3 and the code is working fine, but the problem is when I call codeAddress function through dropdown change event. It increases the marker in the map. I am creating a marker in codeAddress that''s increased on every call of the function codeAddress. I want a single marker displayed at every call of my codeAddress function.
Here is my code:
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
draggable: true,
position: results[0].geometry.location
});
google.maps.event.addListener(marker, 'drag', function(event) {
document.getElementById('<%= txtLatitude.ClientID %>').value = marker.getPosition().lat();
document.getElementById('<%= txtLongitude.ClientID %>').value = marker.getPosition().lng();
document.getElementById('<%= HFLat.ClientID %>').value = marker.getPosition().lat();
document.getElementById('<%= HFLng.ClientID %>').value = marker.getPosition().lng();
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
<select name="address" id="address" onchange="codeAddress();" >
<option value="India">India</option>
<option value="Australia">Australia</option>
<option value="Pakistan">Pakistan</option></select>
有人可以帮我吗?
Can anyone help me please?
推荐答案
var myPermanentMarker = new google.maps.Marker({
map: map,
draggable: true
});
google.maps.event.addListener(myPermanentMarker , 'drag', function(event){
document.getElementById('<%= txtLatitude.ClientID %>').value = myPermanentMarker.getPosition().lat();
document.getElementById('<%= txtLongitude.ClientID %>').value = myPermanentMarker.getPosition().lng();
document.getElementById('<%= HFLat.ClientID %>').value = myPermanentMarker.getPosition().lat();
document.getElementById('<%= HFLng.ClientID %>').value = myPermanentMarker.getPosition().lng();
});
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
myPermanentMarker.position = results[0].geometry.location;
map.setCenter(results[0].geometry.location);
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}
});
这篇关于调用javascript函数时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!