【问题标题】:how to get longitude and latitude of google maps in javascript如何在javascript中获取谷歌地图的经度和纬度
【发布时间】:2016-05-17 09:22:05
【问题描述】:

我正在使用带有java脚本的谷歌地图它工作正常我需要获取当前的经度和纬度并将其保存到数据库中,诀窍是指针是可拖动的,所以我怎样才能获得当前的经度和纬度?

function markernew(){


markers = [];
//var val=document.getElementById('val2').value=;
 $("#floating-panel").show();
   var latlng = new google.maps.LatLng(-34.397, 150.644);
      var myOptions = {
        zoom: 1,
        center: latlng,
      mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
      navigationControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
      if(marker) marker.setMap(null);
      var geocoder;
      var address ="";
  if(!document.getElementById('address_id').value){
 alert('Enter Address');
  }
  else if (!document.getElementById('city_id').value){
 alert('Enter City');
  }
  else if (!document.getElementById('nearby_id').value){
   alert('Enter Country');
  }
  else{
     $("#findplace").show();
    var address="";
   var array = document.getElementById('address_id').value;
   array+=",";

   if(array.match(/,/g).length==1){
  array = array.split(',');
   address += array[0]+" ";
    address += array[1];

   }

   else if(array.match(/,/g).length==2){
    array = array.split(',');
     address += array[0]+" ";
    address += array[1]+" ";
    address += array[2];

   }
   else if(array.match(/,/g).length==3){
    array = array.split(',');
     address += array[0]+" ";
    address += array[1]+" ";
    address += array[2]+" ";
      address += array[3];

   }
    else if(array.match(/,/g).length==4){
     array = array.split(',');
     address += array[0]+" ";
    address += array[1]+" ";
    address += array[2]+" ";
      address += array[3]+" ";
          address += array[4];
   }
   else if(array.match(/,/g).length==5){
     array = array.split(',');
     address += array[0]+" ";
    address += array[1]+" ";
    address += array[2]+" ";
      address += array[3]+" ";
     address += array[4]+" ";
  address += array[5];
   }
   else{
    alert("inside else");
   var address= document.getElementById('address_id').value; 
          }

  // address += document.getElementById('address_id').value + " ";
    address += document.getElementById('city_id').value + " ";
    address += document.getElementById('nearby_id').value;

    geocoder = new google.maps.Geocoder();

    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
          map.setCenter(results[0].geometry.location);

            map.setZoom(15);
            var infowindow = new google.maps.InfoWindow(
                { content: '<b>'+address+'</b>',
                  size: new google.maps.Size(150,50)
                });

            google.maps.event.addListener(map, 'click', function(event) {

    if(abc==true){
     placeMarker(event.latLng);
     abc=false;
      }
     });

          } else {
            alert("No results found");
          }
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
  }

  }
   function placeMarker(location) {

    var marker = new google.maps.Marker({
        position: location, 
        map: map,
  draggable:true
    });
 markers.push(marker);


  var infowindow = new google.maps.InfoWindow({
    content: 'Latitude: ' + location.lat() + '<br>Longitude: ' + location.lng()
  });
  infowindow.open(map,marker);
}
function setMapOnAll(map) {
  for (var i = 0; i < markers.length; i++) {
    markers[i].setMap(map);
  }
}
function clearMarkers() {
  setMapOnAll(null);
}

function deleteMarkers() {
  clearMarkers();
  abc=true;
  markers = [];
}

【问题讨论】:

  • 您可以尝试使用 navigator.geolocation
  • 你有我们可以尝试的回购吗?

标签: javascript google-maps


【解决方案1】:

您可以尝试像这样对 dragend 事件进行 cathc 处理

var latlng = new google.maps.LatLng(-34.397, 150.644);
var marker   = new google.maps.Marker({
    position: latlng ,
    map: map,
    draggable: true   
});

google.maps.event.addListener(marker, 'dragend', function(ev){
    alert(m.getPosition()); // Lat lon after drag
});

希望对你有帮助

【讨论】:

  • 它应该有助于我研究它,我发现代码可以使用这个逻辑,但在我的情况下它不起作用
【解决方案2】:

先去谷歌开发者网站#google developer

有从地址、城市名称获取经纬度的所有描述

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    相关资源
    最近更新 更多