error:InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lat: not a number
Asked Answered
R

5

38
function initAutocomplete() {
    var lat=document.getElementById('lat').value;
    var lng=document.getElementById('lng').value;
    console.log(lat);
    console.log(lng);


    var map = new google.maps.Map(document.getElementById('map'), {
      center: {lat:lat, lng:lng},
      zoom: 13,
      mapTypeId: 'roadmap'
    });}

it gives me the following error :

error:InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lat: not a number

Repairman answered 3/7, 2017 at 5:41 Comment(3)
.value returns a string -> parseFloat()Flanna
The error solved but can u help to locate the location..actually the location is howing on the map but how to show the marker of that location? .thanksRepairman
Post it as a separate question (which should include a minimal, complete, and verifiable example (but don't post your API key ;))Flanna
F
74

The .value attribute of a HTMLInputElement returns the value as a string.

You have to parse the content of lat and lng with parseFloat() before passing it to the maps API

function initAutocomplete() {
    var lat = parseFloat(document.getElementById('lat').value);
    var lng = parseFloat(document.getElementById('lng').value);

    var map = new google.maps.Map(document.getElementById('map'), {
        center: {
            lat: lat,
            lng: lng
        },
        zoom: 13,
        mapTypeId: 'roadmap'
    });
}
Flanna answered 3/7, 2017 at 6:1 Comment(0)
F
8

Just add "+" before your variables:

function initAutocomplete() {
var lat=document.getElementById('lat').value;
var lng=document.getElementById('lng').value;

var map = new google.maps.Map(document.getElementById('map'), {
  center: {lat: +lat, lng: +lng},
  zoom: 13,
  mapTypeId: 'roadmap'
});}
Fluorescence answered 16/3, 2018 at 14:48 Comment(0)
C
1

Same error, slightly different scenario:

I kept getting this error (same as the OP) when trying to use the autocompleted lat/lng to update a map:

    var place = autocomplete.getPlace();
    var geo = place.geometry.location;
    loadMapAt(new google.maps.LatLng(geo.lat, geo.lng));

This "one weird trick" fixed it. Replacing the second line with:

    var geo = JSON.parse(JSON.stringify(place.geometry.location));
Corsica answered 11/10, 2020 at 17:49 Comment(0)
Z
1

I also was getting this error when using autocomplete which the above suggestions didn't solve. In my case, I was able to correct this by passing lat and lng as properties to my Gmaps element:

<Gmaps width={width}
       height={height}
       lat={lat}
       lng={lng}
       params = {{
           v: '3.exp',
           libraries: 'geometry, visualization, places', 
           key: GOOGLE_MAPS_API_KEY
       }}
       onMapCreated={this.onMapCreated}/>

This was using react-gmaps version 1.4.2.

Zenger answered 28/4, 2021 at 20:20 Comment(0)
A
0

Just initialize parameters:

function initMap(lat_ = 0, lon_ = 0) {
    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: lat_, lng: lon_},
        zoom: 10
    });
}
Augusto answered 4/6, 2020 at 4:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.