0

I'm somewhat stuck at pset 8... not really "stuck" stuck as so far my code is working the way it should, but seeing how I am a bit confused in how objects work, I guess I must have been lucky (or ofc I am a wunderkind ;) )

Anyway: In scripts.js the function addMarker is declared and afterwards called in the function update.

for (var i = 0; i < data.length; i++)
    {
        addMarker(data[i]);
    }

In this for loop a number of objects are created by passing in the return value data[i] (which are the objects returned in json by $.getJSON("update.php", parameters)

function addMarker(place)
{   
    // GPS coordinates for marker 
    var latLng = new google.maps.LatLng(place.latitude, place.longitude);

    // Create marker
    var marker = new google.maps.Marker({
        position: latLng,
        map: map,
        title: place.place_name,
        text: place.place_name, 
    });
}

In addMarker objects are created (in the markers global?) which have as properties: latLng and marker, the latter which is an object as well and has the keys: position, map, title, text

My questions are

  1. Is my reasoning correct?
  2. In the specification, it is said that maximum 10 locations will be mapped, where is this indicated in the code?
  3. Where is the global var markers used?
  4. Good design aside, the info window can potentially be a property in marker or an object in itself?
1

Answers

1. Yes.

The following function

function addMarker(place)
{   
    // GPS coordinates for marker 
    var latLng = new google.maps.LatLng(place.latitude, place.longitude);

    // Create marker
    var marker = new google.maps.Marker({
        position: latLng,
        map: map,
        title: place.place_name,
        text: place.place_name, 
    });
}

creates objects of type google.maps.Marker from the objects (place objects) received as arguments. The marker object has properties position, title etc.

Read https://developers.google.com/maps/documentation/javascript/markers

2.

Line 30 and 35 of update.php have the query ending with LIMIT 10 that limits the number of rows selected from the places table.

3.

The global variable markers is declared to keep track of all the markers that are created in addMarker() function. To implemented this in addMarkers() function try something like markers.push(marker). The global markers variable is used in removeMarker() function to remove all the markers from the map before adding new ones.

4.

The info window is an object in itself. It can be attached to a marker to respond to specific events.

Adding info window as a property of a marker object would over complicate things as every marker object does not need a info window. And then to create a info window you might need to create a marker object if that was the case.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .