My code: http://jsbin.com/epuxu
With help from SO, I managed to get addresses geocoded and their according pins placed on the map. The problem is that I can’t select the coordinates in order to append a #message div to it on the map because I don’t have the coordinates anymore.
I suspect I’m doing something wrong in this section:
/* Message
--------------------*/
$("#message").appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));
function displayPoint(marker, index){
$("#message").hide();
var moveEnd = GEvent.addListener(map, "moveend", function(){
var markerOffset = map.fromLatLngToDivPixel(marker.getLatLng());
$("#message")
.fadeIn()
.css({ top:markerOffset.y, left:markerOffset.x });
GEvent.removeListener(moveEnd);
});
map.panTo(marker.getLatLng());
}
it works when I use the original coordinate code (this is commented out on jsbin):
var markers = [
[39.729308,-121.854087],
[39.0,-121.0]
];
for (var i = 0; i < markers.length; i++) {
var point = new GLatLng(markers[i][0], markers[i][1]);
marker = new GMarker(point);
map.addOverlay(marker);
markers[i] = marker;
}
but I need help getting it to work with this current code:
function showAddress(markers) {
if (geocoder) {
geocoder.getLatLng(markers,
function(point) {
if (!point) {
alert(markers + " not found");
} else {
marker = new GMarker(point);
map.addOverlay(marker);
markers[i] = marker;
}
}
);
}
}
for (var i = 0; i < markers.length; i++) {
showAddress(markers[i]);
}
I’m kinda new to utilizing the google maps api, so any insight on what I’m doing wrong would be very helpful. Thanks =]
I’m afraid there were quite a few things wrong with your code 😛
I had to do a bit of plastic surgery but here’s the result: http://jsbin.com/atofe
The following is a description of the changes I made. Let me know if you need help understanding anything.
I had to comment this out since it was causing an error. I’m guessing you left it in there by mistake.
Since we are using addresses instead of coordinates, you don’t need to (in fact you shouldn’t because it only complicates things) encapsulate each string in an array. I also renamed it to
addressesto make it more clear and prevent conflicts with the actual markers (more on that later).This is where I had to make the most changes.
You made a couple of significant mistakes here.
First, you re-used the variable
markerswhen you should have used a new name. In the process you wrote over the array of address strings and misunderstood where things were stored (This is why I renamed the array of address strings toaddresses).Second, you tried to add the markers to the list before the geocoder actually returned its response. I think you didn’t realize that
getLatLngis an asynchronous function, so it executes the callback function only after the geocoder returns its response. Since you didn’t wait for the response, it rendered the “Add markers to list” section useless as the markers had not been retrieved yet.So, to fix these issues I moved the “Add markers to list” section inside the new
handleGeocoderResponsefunction. This ensures the markers are added to the list only after the geocoder response is returned. I also had to use a double-closure since we are using a loop along with an asynchronous function.