3

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?

Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134

2 Answers2

1

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
  • 779
  • 5
  • 11
0

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
  • 8,576
  • 33
  • 117
  • 202