简体   繁体   English

找不到当前位置-Google Maps API

[英]Current location is not being found - Google Maps API

I am learning google maps API and am trying to find the current location along with the marker on the map. 我正在学习Google Maps API,并试图在地图上找到当前位置以及标记。 Marker is working perfectly but current location part is not working. 标记工作正常,但当前位置部分无效。 My code is attached below.Please tell me where i am wrong. 我的代码附在下面。请告诉我我错了。

<script>
var map;
var myCenter = new google.maps.LatLng(13.0827, 80.2707)


var map;
function initialize() {
  var myLatlng = myCenter;
  var mapOptions = {
    zoom: 4,
    center: myLatlng
  }
  var map = new google.maps.Map(document.getElementById('map'), mapOptions);

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      draggable: true
      });
      // To add the marker to the map, call setMap();
      marker.setMap(map);

  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

      var infowindow = new google.maps.InfoWindow({
        map: map,
        position: pos,
        content: 'Great! your location is found.'
      });

      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }
}


function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Error: The Geolocation service failed.';
  } else {
    var content = 'Error: Your browser doesn\'t support geolocation.';
  }

  var options = {
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content
  };

  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

If you check in browser console, it shows the error because map variable is not defined inside handleNoGeolocation() function 如果在浏览器控制台中签入,则会显示错误,因为未在handleNoGeolocation()函数中定义map变量

You can just use map variable defined outside both functions Initialize() and handleNoGeolocation(). 您可以只使用在函数Initialize()和handleNoGeolocation()之外定义的映射变量。

Updated JS Code: 更新了JS代码:

var map;
var myCenter = new google.maps.LatLng(13.0827, 80.2707)

function initialize() {
    var myLatlng = myCenter;
    var mapOptions = {
        zoom: 4,
        center: myLatlng
    }
    // No declaration of local map variable here
    map = new google.maps.Map(document.getElementById('map'), mapOptions);

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        draggable: true
    });
    // To add the marker to the map, call setMap();
    marker.setMap(map);

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

            var infowindow = new google.maps.InfoWindow({
                map: map,
                position: pos,
                content: 'Great! your location is found.'
            });

            map.setCenter(pos);
        }, function () {
            handleNoGeolocation(true);
        });
    } else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
    }
}


function handleNoGeolocation(errorFlag) {
    if (errorFlag) {
        var content = 'Error: The Geolocation service failed.';
    } else {
        var content = 'Error: Your browser doesn\'t support geolocation.';
    }

    var options = {
        map: map,
        position: new google.maps.LatLng(60, 105),
        content: content
    };

    var infowindow = new google.maps.InfoWindow(options);
    map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM