Link to home
Start Free TrialLog in
Avatar of Brad Bansner
Brad Bansner

asked on

Google Maps API V3 placing marker on map using Latitude and Longitude values from a variable

This URL displays a marker on a Google Map:
http://tech.lifespanfitness.com/store-locator.asp?p=19565

This URL does not:
http://tech.lifespanfitness.com/store-locator2.asp?p=19565

The relevant Javascript is shown below, for the two URLs above (respectively). However, there is only one line that is different between them:

position:new google.maps.LatLng(40.33897,-75.98161),
...vs.
position:new google.maps.LatLng(currentlatlng),

In the first, I am hard-coding Latitude and Longitude values. In the second, I am populating them with the variable currentlatlng. The two console.log statements show what these two values are (see screenshot). Of course, in my variable, you have to open the object to see what is inside, and it contains data other than just the Latitude and Longitude... I am guessing that is the problem, but I don't know how to send the API what it is looking for.

So my question is, how can I make the Google Map API display my marker using the value in the currentlatlng variable instead of having to hard code? It seems like I am 99% of the way there, but can't seem to get this to work.

Thank you!

function initialize(){
	var mapOptions={
		center:new google.maps.LatLng(40.33897,-75.98161),
		zoom:9,
		mapTypeId:google.maps.MapTypeId.ROADMAP
	}

	var map=new google.maps.Map(document.getElementById('googleMap'),mapOptions);
	var geocoder=new google.maps.Geocoder();
	var currentlatlng;

	geocoder.geocode({'address':'1050 Berkshire Blvd, Wyomissing, PA 19610, United States'},function(results, status){
		if (status==google.maps.GeocoderStatus.OK){
			currentlatlng=results[0].geometry.location;
			console.log(currentlatlng);
			console.log('40.33897,-75.98161');

			var marker0=new google.maps.Marker({
				position:new google.maps.LatLng(40.33897,-75.98161),
				map:map,
				title:'Play It Again Sports - Wyomissing'
			});
		}
	});
}

google.maps.event.addDomListener(window, 'load', initialize);

Open in new window


function initialize(){
	var mapOptions={
		center:new google.maps.LatLng(40.33897,-75.98161),
		zoom:9,
		mapTypeId:google.maps.MapTypeId.ROADMAP
	}

	var map=new google.maps.Map(document.getElementById('googleMap'),mapOptions);
	var geocoder=new google.maps.Geocoder();
	var currentlatlng;

	geocoder.geocode({'address':'1050 Berkshire Blvd, Wyomissing, PA 19610, United States'},function(results, status){
		if (status==google.maps.GeocoderStatus.OK){
			currentlatlng=results[0].geometry.location;
			console.log(currentlatlng);
			console.log('40.33897,-75.98161');

			var marker0=new google.maps.Marker({
				position:new google.maps.LatLng(currentlatlng),
				map:map,
				title:'Play It Again Sports - Wyomissing'
			});
		}
	});
}

google.maps.event.addDomListener(window, 'load', initialize);

Open in new window

1.jpg
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brad Bansner
Brad Bansner

ASKER

Ah, of course. That makes sense. Thank you!