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

StackOverflow https://stackoverflow.com/questions/73524

  •  09-06-2019
  •  | 
  •  

Question

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?

Was it helpful?

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top