Jack The Baker
Jack The Baker

Reputation: 1883

Google maps show wrong location on input

I am using Google Maps API and it get lat long via hidden text box. Today it acting odd, it show wrong location on map, but if I write lat long manually for lat and long variable, it works good.

Html:

<input type="hidden" name="ads-lat" id="ads-lat" value="35.68969456776873">
<input type="hidden" name="ads-long" id="ads-long" value="51.46769173739062">

Not working:

var long = document.getElementById("ads-long").value;
var lat = document.getElementById("ads-lat").value;

Not working

Working good:

var long = 51.46769173739062;
var lat = 35.68969456776873;

Working

Google code:

map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: +lat,
lng: +long
},
zoom: 15,
streetViewControl: false,
styles : styles, 
fullscreenControl: true
});

I have no idea why it acting like this, there is no error in console, also I checked with two different browser, it's not cache. As you see, it working when I write lat and long manually, it's kind of number/decimal, right? assuming it get value from input as string, but I write a plus sign + before variable, I repeat, there is no error about not a number from google. Any idea?

JSFiddle

It show somewhere in Russia (I think) but it's wrong, it should show somewhere in Iran, You can check lat long on this website.

Upvotes: 0

Views: 1582

Answers (1)

designosis
designosis

Reputation: 5263

How's this?

http://jsfiddle.net/f09bteov/19/

I did two things: 1) Changed parseInt to parseFloat (you were getting lat/lng of 35,51 ... ~170km area per degree, so a bit off), 2) Correct their order, and 3) Removed the + in your lat: +lat, lng: +long declarations. Not sure why they were there.

Upvotes: 1

Related Questions