Solved

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

Posted on 2013-06-19
2
490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

730 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