Javascript 如何使用在文本框中传递的输入经度和纬度值在谷歌地图上获取位置?

Javascript 如何使用在文本框中传递的输入经度和纬度值在谷歌地图上获取位置?,javascript,google-maps,geolocation,Javascript,Google Maps,Geolocation,我试图在谷歌地图上找到位置 我在文本框中输入地名,然后按submit按钮成功查找位置 但如何在文本框中输入经度和纬度来查找位置呢? 我试图在文本框中传递经度和纬度值,如-26.958405,24.729860,但在地图上显示位置并不能显示准确的结果。 那么,如何使用传递到textbox的经度和纬度值查找位置呢 <input type="text" id="addressinput" class="form-control col-md-6 col-xs-12"> <inpu

我试图在谷歌地图上找到位置 我在文本框中输入地名,然后按submit按钮成功查找位置 但如何在文本框中输入经度和纬度来查找位置呢? 我试图在文本框中传递经度和纬度值,如-26.958405,24.729860,但在地图上显示位置并不能显示准确的结果。 那么,如何使用传递到textbox的经度和纬度值查找位置呢

<input type="text"   id="addressinput" class="form-control col-md-6 col-xs-12">
<input id="Button1" type="button" value="view" onclick="return Button1_onclick()" class="btn btn-primary" />


var映射;
var地理编码器;
函数初始化映射(){
var-latlng=new google.maps.latlng(-29.83395963059587430.35492856055498);
变异性肌肽=
{
缩放:9,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP,
disableDefaultUI:true
};
map=新的google.maps.map(document.getElementById(“map”),myOptions);
}
函数FindLocaiton(){
geocoder=新的google.maps.geocoder();
初始化映射();
var address=document.getElementById(“addressinput”).value;
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
map.setCenter(结果[0].geometry.location);
var marker=new google.maps.marker({
地图:地图,
位置:结果[0]。几何体。位置
});
}
否则{
警报(“地理编码因以下原因未成功:“+状态”);
}
});
}
功能按钮1_onclick(){
FindLocaiton();
}
window.onload=初始化映射;

假设您在myLatLng Var中有lat、lang,您可以这样使用您的gecoder

var myLatLng = new google.maps.LatLng(-29.833959630595874, 30.35492856055498);
google.maps.Geocoder().geocode({'latLng': myLatLng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
  if (results[0]) {
    alert(results[0].formatted_addres);
}
else {
  alert( "Geocoder impossible");
}

尝试为longitute和latitude值添加两个不同的文本框,并在您的位置上传递值

var address = document.getElementById("addressinput").value;
var address1 = document.getElementById("addressinput1").value;
        geocoder.geocode({ 'address': address },{ 'address1': address1 }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location

                });
我理解你的问题 请尝试使用经度和纬度查找位置的代码,例如反向映射

 <div id="map" style="width: 100%; height: 400px"></div><br/><br/>

 <div id="floating-panel">
 <input id="latlng" type="text" placeholder="Enter map coordinates">
 <input id="submit" type="button" value="view">
 </div>



 <script>
  function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 11,
      center: {lat: 40.731, lng: -73.997}
    });
    var geocoder = new google.maps.Geocoder;
    var infowindow = new google.maps.InfoWindow;

    document.getElementById('submit').addEventListener('click', function() {
      geocodeLatLng(geocoder, map, infowindow);
    });
  }

  function geocodeLatLng(geocoder, map, infowindow) {
    var input = document.getElementById('latlng').value;
    var latlngStr = input.split(',', 2);
    var latlng = {lat: parseFloat(latlngStr[0]), lng: parseFloat(latlngStr[1])};
    geocoder.geocode({'location': latlng}, function(results, status) {
      if (status === 'OK') {
        if (results[1]) {
          map.setZoom(11);
          var marker = new google.maps.Marker({
            position: latlng,
            map: map
          });
          infowindow.setContent(results[1].formatted_address);
          infowindow.open(map, marker);
        } else {
          window.alert('No results found');
        }
      } else {
        window.alert('Geocoder failed due to: ' + status);
      }
    });
  }
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YourGoogleMapKEY">
</script>


函数initMap(){ var map=new google.maps.map(document.getElementById('map'){ 缩放:11, 中心:{lat:40.731,lng:-73.997} }); var geocoder=new google.maps.geocoder; var infowindow=new google.maps.infowindow; document.getElementById('submit')。addEventListener('click',function(){ geocodeLatLng(地理编码器、地图、信息窗口); }); } 功能geocodeLatLng(地理编码器、地图、信息窗口){ var输入=document.getElementById('latlng')。值; var latlngStr=input.split(',',2); var-latlng={lat:parseFloat(latlngStr[0]),lng:parseFloat(latlngStr[1]); geocoder.geocode({'location':latlng},函数(结果,状态){ 如果(状态=='OK'){ 如果(结果[1]){ map.setZoom(11); var marker=new google.maps.marker({ 位置:latlng, 地图:地图 }); infowindow.setContent(结果[1]。格式化的\u地址); 信息窗口。打开(地图、标记); }否则{ window.alert(“未找到结果”); } }否则{ window.alert('地理编码器由于:'+状态而失败); } }); }
首先,每次单击按钮都要创建地图和地理编码器。您应该只创建一次,并在函数之外保留对它们的引用,以便可以重用它们;虽然如果你有坐标,你不需要地理编码器(使用地理编码器会将标记移动到最近的“已知”位置)
 <div id="map" style="width: 100%; height: 400px"></div><br/><br/>

 <div id="floating-panel">
 <input id="latlng" type="text" placeholder="Enter map coordinates">
 <input id="submit" type="button" value="view">
 </div>



 <script>
  function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 11,
      center: {lat: 40.731, lng: -73.997}
    });
    var geocoder = new google.maps.Geocoder;
    var infowindow = new google.maps.InfoWindow;

    document.getElementById('submit').addEventListener('click', function() {
      geocodeLatLng(geocoder, map, infowindow);
    });
  }

  function geocodeLatLng(geocoder, map, infowindow) {
    var input = document.getElementById('latlng').value;
    var latlngStr = input.split(',', 2);
    var latlng = {lat: parseFloat(latlngStr[0]), lng: parseFloat(latlngStr[1])};
    geocoder.geocode({'location': latlng}, function(results, status) {
      if (status === 'OK') {
        if (results[1]) {
          map.setZoom(11);
          var marker = new google.maps.Marker({
            position: latlng,
            map: map
          });
          infowindow.setContent(results[1].formatted_address);
          infowindow.open(map, marker);
        } else {
          window.alert('No results found');
        }
      } else {
        window.alert('Geocoder failed due to: ' + status);
      }
    });
  }
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YourGoogleMapKEY">
</script>