Jump to content

Google Maps, Geocode


miela1
 Share

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
Link to comment
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
Link to comment
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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...