Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-06-19
2
Medium Priority
?
514 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

670 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