apiyahoo-maps

How do I convert from a location (address) String to a YGeoPoint in Yahoo Maps API?


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?


Solution

  • 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.