【问题标题】:How to pass the ajax result to google maps如何将ajax结果传递给谷歌地图
【发布时间】:2012-09-18 00:30:58
【问题描述】:

我正在使用此代码获取当前位置的经纬度。

<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;    
  }
</script>

我需要将此 ajax 代码中的经度和纬度传递给谷歌地图的 LatLng 变量,如何做到这一点。

这是我正在使用的谷歌地图 api 代码

    function initialize() {
  var latlng = new google.maps.LatLng(52.000,17.1100);
    var myOptions = {
      zoom: 12,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

谁能帮帮我..提前谢谢:)

【问题讨论】:

    标签: javascript ajax google-maps


    【解决方案1】:
    function showPosition(position)
    {
        x.innerHTML="Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude;
        initialize(position);
    }
    
    function initialize(position) {
        var latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
        var myOptions = {
        zoom: 12,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    

    【讨论】:

      【解决方案2】:

      如果你已经初始化了地图,你可以在获取位置后将其居中:

      map.setCenter(new google.maps.LatLng(position.coords.latitude,position.coords.longitude));
      

      【讨论】:

        【解决方案3】:

        这应该只是传递您已经检索到的经纬度的情况:

        function showPosition(position)
        {
         var latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
         var myOptions = {
          zoom: 12,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
         };
        
        }
        

        【讨论】:

          【解决方案4】:

          与您当前的方式相同,但传入 position.coords.latitudeposition.coords.longitude 而不是 52.00017.1100

          var latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-01-09
            • 2016-07-02
            • 2018-06-11
            • 1970-01-01
            • 2017-04-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多