Jump to content
Sign in to follow this  
miela1

Google Maps, Geocode

Recommended Posts

Hej, i don't understandt, why, this dosent work, i am trying to make an factory to add locations to my map, but i am new to the google maps API, and not thatb adv. in JavaScript. i know that if it had bin an c# function it will work. hope sombody can help, and sorry for my pure spelling skills,

function calcLatLng(adresse) {	var Local = new google.maps.LatLng;	geocoder.geocode({ 'address': adresse }, function (results, status) {		if (status == google.maps.GeocoderStatus.OK) {			Local = results[0].geometry.location;			console.log(Local); //this log the corect result,		} else { 		}	});// if i make an 'console.log' here only gets a empty 'google.maps.LatLng' object	return Local;}function addNewPlace(info, icon, adresse, title) {	var marker = new google.maps.Marker({		map: map,		position: calcLatLng(adresse), // but here i only gets a 'undefind'		title: title,		icon: icon	});	var infowindow = new google.maps.InfoWindow({		content: info	});	infowindow.open(map, marker);}

Edited by miela1

Share this post


Link to post
Share on other sites

A lot of things in Javascript are asynchronous, meaning that they don't necessarily finish before the code moves on. In that code above, using geocoder.geocode sends an asynchronous request to another server to do the calculation. The browser doesn't wait for that to finish, it moves on so it executes the return statement before that request finishes. When the request finishes then it runs the function that you passed to geocode. So anything that you want to happen after that request finishes needs to happen in that callback function. If the request succeeded then you can call your addNewPlace function and send it the position that was returned.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks. now, that you say that, i don't know why, i did not think of it my self. but then agen that's why forums like this exist, as humans we get stuck in one way of thinking, and need input from an other person to get un stuck.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...