google getLocation function not work in my script

2019-08-26 09:43发布

Hi all I need to show a google map in my site with my home like marker ( I use a store position for it) and centered in a user position. I try with:

<script>
        var myCenter = new google.maps.LatLng('xxx');
        var userCenter;

            function getLocation() {
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(showPosition);
                } 
            }

            function showPosition(position) {
                var latlon = position.coords.latitude + "," + position.coords.longitude;
                userCenter=new google.maps.LatLng(latlon);
            }

        var marker;

        function initialize()
        {
            var mapProp = {
              center: userCenter,
              zoom:15,
              mapTypeId:google.maps.MapTypeId.ROADMAP
              };

            var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

            marker=new google.maps.Marker({
              position:myCenter,
              });

            marker.setMap(map);

            var infowindow = new google.maps.InfoWindow({
                content:"Casa"
            });

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(map,marker);
            });
        }                       

        google.maps.event.addDomListener(window, 'load', initialize);

    </script>

if I don't use userCenter and use MyCenter the map works and show it centered in MyCenter.

1条回答
劳资没心,怎么记你
2楼-- · 2019-08-26 10:32

The problem is the order of execution of the functions. You want to use userCenter before userCenter is available. This produces an error ( on var mapProp = {center: userCenter, ...} ) and so initialize stops working.

By the way, you never call getLocation(). A function that is only defined, does nothing. A function only does something if you call it somewhere.

(By the way 2: navigator.geolocation is not a Google service. It is a service that the web browser provides)

I put extra comment in the code

<script src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script>
    var myCenter = new google.maps.LatLng(50.845463, 4.357112);
    var userCenter;
    var marker;
    var map;

    // sends a request to get the location of the user.
    // Notice: you will have to wait for the callback  (= showPosition) before you have the result of this request.
    // So you cannot rely only on userCenter.  You must use myCenter, until you are sure that showPosition receives the response
    function getUserLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
      }
    }

    // Response of getUserLocation().  The location of the client is known.
    // Notice: by this time initialize() has been called.  initialize() doesn't know anything about userCenter.  
    // userCenter will only get a value when this function is called.
    function showPosition(position) {
      userCenter = new google.maps.LatLng(
        position.coords.latitude,
        position.coords.longitude
      );
      // Now we know the position of the client.  We can set the center of the map to this location
      map.setCenter(userCenter);
    }

    // this function initializes Google maps.  It is triggered the moment the DOM of the web page is loaded.
    function initialize() {
      // let's start the request to get the position of the user
      getUserLocation();

      var mapProp = {
        center: myCenter,  // notice: userCenter is still undefined, you cannot use it yet.
        zoom:15,
        mapTypeId:google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
      marker = new google.maps.Marker({
        position: myCenter,
        map: map   // this replaces  marker.setMap(map);
      });

      var infowindow = new google.maps.InfoWindow({
        content: "Casa"
      });
      // a click on the marker opens the infoWindow
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>
查看更多
登录 后发表回答