Google Maps Tutorial - Part 2
Next installment of the Google Maps tutorial series.
First we start with the starting point google map implementation, from the last post.
Feel free to peek ahead to the final google map implementation after this interation. Make sure you left click on the map, and drag and drop the resulting markers.
In this iteration I will add map overlays, actualy two kinds of overlays: markers (GMarker) and lines (GPolyline), that are provided by the Google Maps API.
Also I'll add event handler to handle mouse click events using the GEvent API.
First, I added a function to draw a line and store it in a array called lines. Drawing lines is as simple as creating a GPolyline object and adding it to the map using the addOverlay method.
-
function plotLine(pt1, pt2) {
-
var polyline = new GPolyline([
-
pt1, pt2
-
], "#0000ff", 5, 0.5);
-
map.addOverlay(polyline);
-
lines.push(polyline);
-
}
Next I added a plot function to plot the entire boundary, which is stored in the boundary array. In the plot function I first delete all the lines so far, useful if the user changes the boundary by dragging and dropping the markers, and calling the above plotLine function with the array of points in the current boundary variable.
Note how I can remove existing overlays by invoking the removeOverlay method of map.
-
function plot() {
-
if (lines.length> 0) {
-
for (var idx in lines) {
-
map.removeOverlay(lines[idx]);
-
}
-
lines = new Array();
-
}
-
-
for (var i=0; i+1<boundary.length; i++) {
-
plotLine(boundary[i].getLatLng(),
-
boundary[i+1].getLatLng());
-
}
-
}
Finally I tie everything together by adding event handlers for a click event and a dragend event. The click event will place a new marker, which can then be subsequently moved by dragging and dropping it. The Google Maps API comes with a default marker icon when a new marker (GMarker object) is created. This can be overwridden with whatever icon you want by creating a GIcon object which I did here for the first placed marker.
Note how the addListener method is given a callback function which gets a pointer to the overlay and the point itself on a click event. The overlay is checked to see whether we clicked on an already existing overlay (if so do nothing), and the point is used to create a GMarker object and add it to the map using map.addOverlay.
-
GEvent.addListener(map, "click", function(overlay,point) {
-
if (overlay != null) {
-
} else {
-
var n = boundary.length;
-
if (first_point) {
-
var func_icon = new GIcon();
-
func_icon.image =
-
"http://gsacs1.enterprisedemo-google.com/googleMaps/green.png";
-
func_icon.iconSize = new GSize(26, 26);
-
func_icon.shadowSize = new GSize(20, 20);
-
func_icon.iconAnchor = new GPoint(14, 24);
-
func_icon.infoWindowAnchor = new GPoint(15, 1);
-
boundary.push(new GMarker(point,
-
{icon: func_icon, draggable: true, clickable : true}));
-
first_point = false;
-
} else {
-
boundary.push(new GMarker(point,
-
{draggable: true, clickable : true}));
-
}
-
map.addOverlay(boundary[n]);
-
GEvent.addListener(boundary[n], "dragend",
-
function() {
-
plot();
-
});
-
-
plot();
-
}
-
});
And that's it, for this iteration. Check out the final product, and stay tuned for the next iteration. Also, you might want to check out the plan for the entire Google Maps Taxi application.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.



Comments
No comments yet.
Leave a comment