I have a list of addresses from a Database for which I'd like to put markers on a Yahoo Map. The addMarker()
method on YMap takes a YGeoPoint, which requires a latitude and longitude. However, Yahoo Maps must know how to convert from addresses because drawZoomAndCenter(LocationType,ZoomLevel)
can take an address. I could convert by using drawZoomAndCenter()
then getCenterLatLon()
but is there a better way, which doesn't require a draw?
views:
678answers:
3
A:
If you're working with U.S. addresses, you can use geocoder.us, which has APIs.
Also, Google Maps Hacks has a hack, "Hack 62. Find the Latitude and Longitude of a Street Address", for that.
Paul Reiners
2008-09-16 15:32:18
This is only readable if you have a Safari account - so no use to me.
Nick Fortescue
2008-09-16 16:45:45
+1
A:
You can ask the map object to do the geoCoding, and catch the callback:
<script type="text/javascript">
var map = new YMap(document.getElementById('map'));
map.drawZoomAndCenter("Algeria", 17);
map.geoCodeAddress("Cambridge, UK");
YEvent.Capture(map, EventsList.onEndGeoCode, function(geoCode) {
if (geoCode.success)
map.addOverlay(new YMarker(geoCode.GeoPoint));
});
</script>
One thing to beware of -- in this example the drawAndZoom
call will itself make a geoCoding request, so you'll get the callback from that too. You might want to filter that out, or set the map's centre based on a GeoPoint.
David Bick
2008-09-17 09:47:11