Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

javascript - How can I use one marker for multiple functions

I'm trying to make a geocoding function and reverse geocoding function in the same html page. I need to use just one marker for these functions. The code below works but every time I call a function, I create a new marker. What I need is just one marker to control in the whole map

<!DOCTYPE html>
 <html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Geocoding service</title>
    <style>
     html, body, #map-canvas {
    height: 100%;
    margin: 0px;
    padding: 0px
  }
  #panel {
    position: absolute;
    top: 5px;
    left: 50%;
    margin-left: -180px;
    z-index: 5;
    background-color: #fff;
    padding: 5px;
    border: 1px solid #999;
  }
</style>
   <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(34.042145, -4.997128);
 var mapOptions = {
zoom: 5,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      marker = new google.maps.Marker({
      map: map,
      position: latlng,
      draggable : true
  });
google.maps.event.addListener(marker,"dragend",function(){

lat = marker.getPosition().lat();
 document.getElementById("latitude").value = lat ;
lg= marker.getPosition().lng();
 document.getElementById("longitude").value = lg ;
});
   }

function codeAddress() {
var address = document.getElementById('address').value;
var lg;
var lat;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
  map.setCenter(results[0].geometry.location);

  map.setZoom(16);

   var lat = results[0].geometry.location.lat();
   document.getElementById('latitude').value = lat;  

   var lg = results[0].geometry.location.lng();
   document.getElementById('longitude').value = lg;


      marker = new google.maps.Marker({
      map: map,
      position: results[0].geometry.location,
      draggable : true
  });
google.maps.event.addListener(marker,"dragend",function(){

lat = marker.getPosition().lat();
document.getElementById("latitude").value = lat ;
lg= marker.getPosition().lng();
document.getElementById("longitude").value = lg ;

});
} 

  else {
  alert('Geocode was not successful for the following reason: ' + status);
}
});
}

function codeLatLng() {
var input = document.getElementById('latlng').value;
var latlngStr = input.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
  if (results[1]) {
    map.setCenter(latlng);
    map.setZoom(11);
     marker = new google.maps.Marker({
        position: latlng,
        map: map
    });
    document.getElementById("adresse").value =  results[1].formatted_address;

  } else {
    alert('No results found');
  }
} else {
  alert('Geocoder failed due to: ' + status);
}
});
}


google.maps.event.addDomListener(window, 'load', initialize);

 </script>
 </head>
 <body>
 <div id="panel">
  <input id="address" type="textbox">
  <input id="latitude" type="textbox" >
  <input id="longitude" type="textbox" >
  <input type="button" value="Geocode" onclick="codeAddress()">
  <input id="latlng" type "textbox" > 
        <input type="button" value="Reverse Geocode" onclick="codeLatLng()">
  <input id="adresse" type "textbox" > 

</div>
<div id="map-canvas"></div>
</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Within your codeAddress() function you'll need to check if marker exists and then create it if it doesn't or simply set it's position if it doesn't.

To do this, you'll first need to declare a global variable for marker in the same way you currently do for geocoder and map.

Then modify the block of code that creates the marker to the following:

if(marker){
    marker.setPosition(results[0].geometry.location);
}else{
    marker=new google.maps.Marker({
        map:map,
        position:results[0].geometry.location,
        draggable:true
    });
    google.maps.event.addListener(marker,"dragend",function(){
        lat=marker.getPosition().lat();
        document.getElementById("latitude").value=lat;
        lg=marker.getPosition().lng();
        document.getElementById("longitude").value=lg ;
    });
}

EDIT: You'll also need to do something similar within your codeLatLng() function.

ASIDE: Incidentally, you should remove the marker's dragend function within your initialize function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...