首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌地图和使用html5API的逆向地理编码?

谷歌地图和使用html5API的逆向地理编码?
EN

Stack Overflow用户
提问于 2013-12-10 01:07:44
回答 1查看 331关注 0票数 0

为什么这样做:

代码语言:javascript
复制
        function initCoords() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(initialize, locationError);
  } else {
    showError("Your browser does not support Geolocation!");
  }
}
initCoords();
function locationError(){
  alert('"Your browser does not support Geolocation!"');
}
function initialize(position) {
    lat = position.coords.latitude;
    lon = position.coords.longitude;
    var acc = position.coords.accuracy;
    geocoder = new google.maps.Geocoder();
    // Debugging
    console.log(position.coords);
    console.log("Accuracy: "+acc+"\nLatitude: "+lat+"\nLongitude: "+lon);
    var latlng = new google.maps.LatLng(lat,lon);

    // Google Maps API
    var myLatlng = new google.maps.LatLng(lat,lon);
    var mapOptions = {
        center: latlng,
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
}
  function codeLatLng(position) {
   lat = position.coords.latitude;
    lon = position.coords.longitude;
    var latlng = new google.maps.LatLng(lat, lon);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
          map.setZoom(11);
          marker = new google.maps.Marker({
              position: latlng,
              map: map
          });
          infowindow.setContent(results[1].formatted_address);
          infowindow.open(map, marker);
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }
  codeLatLng();

请始终返回以下内容:

代码语言:javascript
复制
Uncaught TypeError: Cannot read property 'coords' of undefined 

在这条线上:

代码语言:javascript
复制
    function codeLatLng(position) {
   lat = position.coords.latitude;
    enter code here

它给我看的是地图,而不是有地址的标记.我也试过这样做:codeLatLng(定位);

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-10 15:13:11

在呈现google地图之后,我将对CodeLatLng()的调用移到Initialize()函数中,并将位置参数传递给它。见上面的评论。

代码语言:javascript
复制
<html>
<head>
 <script type="text/javascript"      src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
 <script>
 function initCoords() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(initialize, locationError);
  } else {
    showError("Your browser does not support Geolocation!");
  }
}
initCoords();
function locationError(){
  alert('"Your browser does not support Geolocation!"');
}
function initialize(position) {
    lat = position.coords.latitude;
    lon = position.coords.longitude;
    var acc = position.coords.accuracy;
    geocoder = new google.maps.Geocoder();
    // Debugging
    console.log(position.coords);
    console.log("Accuracy: "+acc+"\nLatitude: "+lat+"\nLongitude: "+lon);
    var latlng = new google.maps.LatLng(lat,lon);

    // Google Maps API
    var myLatlng = new google.maps.LatLng(lat,lon);
    var mapOptions = {
        center: latlng,
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

  codeLatLng( position );
}
  function codeLatLng(position) {
   lat = position.coords.latitude;
    lon = position.coords.longitude;
    var latlng = new google.maps.LatLng(lat, lon);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
          map.setZoom(11);
          marker = new google.maps.Marker({
              position: latlng,
              map: map
          });
          infowindow.setContent(results[1].formatted_address);
          infowindow.open(map, marker);
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }
 </script>
 </head>
  <body onload="">
  <div id="map_canvas" style="width: 400px; height: 400px;"></div>
  </body>
  </html>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20484128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档