Solved

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

Posted on 2013-06-19
2
475 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
Comment Utility
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
Comment Utility
Ah, of course. That makes sense. Thank you!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now