How to fix Uncaught InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number?

后端 未结 9 734
天命终不由人
天命终不由人 2020-12-13 17:14

I\'m trying to port my googlemaps v2 functions to v3.

But somehow i stuck in a strange error and i could not find, what i\'m doing wrong.

Erro

相关标签:
9条回答
  • 2020-12-13 17:43

    You're probably passing null value if you're loading the coordinates dynamically, set a check before you call the map loader ie: if(mapCords){loadMap}

    0 讨论(0)
  • Using angular-google-maps

    $scope.bounds = new google.maps.LatLngBounds();
    for (var i = $scope.markers.length - 1; i >= 0; i--) {
        $scope.bounds.extend(new google.maps.LatLng($scope.markers[i].coords.latitude, $scope.markers[i].coords.longitude));
    };    
    $scope.control.getGMap().fitBounds($scope.bounds);
    $scope.control.getGMap().setCenter($scope.bounds.getCenter());
    
    0 讨论(0)
  • 2020-12-13 17:48

    you can use this way

     map = new google.maps.Map(document.getElementById('map'), {
            zoom: 16,
            center: { lat: parseFloat(lat), lng: parseFloat(lng) },
            mapTypeId: 'terrain', 
            disableDefaultUI: true
     });
    

    EX : center: { lat: parseFloat(lat), lng: parseFloat(lng) },

    0 讨论(0)
  • 2020-12-13 17:48

    I had the same problem when setting the center of the map with map.setCenter(). Using Number() solved for me. Had to use parseFloat to truncate the data.

    code snippet:

     var centerLat = parseFloat(data.lat).toFixed(0);
     var centerLng = parseFloat(data.long).toFixed(0);
     map.setCenter({
          lat: Number(centerLat),
          lng: Number(centerLng)
     });
    
    0 讨论(0)
  • 2020-12-13 17:54

    I was having the same problem, the fact is that the input of lat and long should be String. Only then did I manage.

    for example:

    Controller.

     ViewBag.Lat = object.Lat.ToString().Replace(",", ".");
    
     ViewBag.Lng = object.Lng.ToString().Replace(",", ".");
    

    View - function javascript

    <script>
        function initMap() {
            var myLatLng = { lat: @ViewBag.Lat, lng: @ViewBag.Lng};
    
            // Create a map object and specify the DOM element for display.
            var map = new window.google.maps.Map(document.getElementById('map'),
            {
                center: myLatLng,
                scrollwheel: false,
                zoom: 16
            });
    
            // Create a marker and set its position.
            var marker = new window.google.maps.Marker({
                map: map,
                position: myLatLng
                //title: "Blue"
            });
        }
    </script>
    

    I convert the double value to string and do a Replace in the ',' to '.' And so everything works normally.

    0 讨论(0)
  • 2020-12-13 17:58

    Uncaught InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number

    Means you are not passing numbers into the google.maps.LatLng constructor. Per your comment:

    /*Information from chromium debugger
    trader: Object
        geo: Object
            lat: "49.014821"
            lon: "10.985072"
    
    */
    

    trader.geo.lat and trader.geo.lon are strings, not numbers. Use parseFloat to convert them to numbers:

    var myLatlng = new google.maps.LatLng(parseFloat(trader.geo.lat),parseFloat(trader.geo.lon));
    
    0 讨论(0)
提交回复
热议问题