Solved

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

Posted on 2013-06-19
2
483 Views
Last Modified: 2013-06-19
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
0
Comment
Question by:bbdesign
2 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 39259475
It already is a LatLng so just use the variable.

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

Author Comment

by:bbdesign
ID: 39259496
Ah, of course. That makes sense. Thank you!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question