Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Build an Interactive Website > Google Maps Integration > Adding Point Markers to a Map > Code Challenge 4/6

Hello,

I'm afraid I have to agree with Gareth Redfern in his post here: http://teamtreehouse.com/forum/task-4-google-maps-integration

It may just be me but I am really struggling with this latest js class. I feel like we >have gone from 10mph to 150mph. Although the class is definitely where I want to >be heading in terms of knowledge I am doing a lot of copying what is in the video >without fully understanding the reasons why I am doing what I am doing if that >makes sense.

Treehouse is amazing, but unlike the other courses, this one lacks explanations. Maybe some "Deep Dives" would help, to lay down some background information?

The Video:

http://teamtreehouse.com/library/websites/build-an-interactive-website/google-maps-integration/adding-point-markers-to-a-map

The Code Challenge Question

On the line after "geocoderOptions" on the "geocoder" object call the "geocode" >method, pass in "geocoderOptions" as the first argument, an anonymous function >as the second. The anonymous function takes two parameters "results" and >"status". If "google.maps.GeocoderStatus.OK" is the same as the status create a >"google.maps.Marker" in "theMap" and at the position of the first element in >"results", like in the video and push it on to the "markers" array.

My Problem

I'm having trouble understanding the last part:

...create a "google.maps.Marker" in "theMap" and at the position of the first element >in "results", like in the video and push it on to the "markers" array.

Specifically, I'm not sure how to create a marker in theMap. I've got a variable called "theMap", but it is not an array. I'm not sure what it means to create something inside of it. I see that the anonymous function within the geocode() method takes a parameter called "results", which seems to be an array. I haven't defined "results" anywhere though, so again, I'm not sure how to create a marker inside of it.

I know this has something to do with addMarkerToMap(), but I don't know how that works.

My Code:

var mapOptions = {
center: new google.maps.LatLng(28.42, -81.58),
zoom: 18,
mapTypeId: google.maps.MapTypeId.SATELLITE
};

var mapElement = document.getElementById("map");
//Here "theMap" is defined. The question asks us to create a marker inside of it.
var theMap = new google.maps.Map(mapElement, mapOptions);
var markers = new Array();

var geocoder = new google.maps.Geocoder();
var bounds = new google.maps.LatLngBounds();

var geocoderOptions = {address: "Magic Kingdom, Disney World"};

//Here is the array "results". We also need to create a marker inside of it.
geocoder.geocode(geoCoderOptions, function(results, status) {
if(status == google.maps.GeocoderStatus.OK)
{var marker = new google.maps.Marker();
markers.push(marker);
}
});

Again, Treehouse is awesome, and overall I'm learning a lot. I appreciate any help.

1 Answer

Hey I was having trouble too, but after reading that post I managed to figure it out...try this:

geocoder.geocode(geocoderOptions, function(results, status){
    if(google.maps.GeocoderStatus.OK == status) {
    var marker = new google.maps.Marker({
        map: theMap,
        position: results[0].geometry.location
    });
    markers.push(marker);
  }
});