Converting address strings to coordinates for Yahoo Maps API markers

I’m working on a project where I need to place markers on a Yahoo Map using addresses from my database. The problem is that the addMarker() function requires a YGeoPoint with latitude and longitude coordinates. I know Yahoo Maps can convert addresses because drawZoomAndCenter() accepts them.

Is there a way to turn my address strings into YGeoPoints without having to use drawZoomAndCenter() followed by getCenterLatLon()? I would prefer not to redraw the map solely to obtain the coordinates.

Here’s a simplified version of what I’m trying to do:

function placeMarkers(addresses) {
  const map = new YMap(document.getElementById('map'));
  
  addresses.forEach(address => {
    // How do I convert an address into a YGeoPoint?
    const point = convertAddressToYGeoPoint(address);
    map.addMarker(point);
  });
}

Any ideas on how to make this work? Thanks for your help!

I’ve actually tackled this issue before in a project. What worked for me was using the YGeoCode class. It’s not as well-documented as other parts of the API, but it’s pretty handy for this exact scenario.

Here’s a rough example of how you could implement it:

function placeMarkers(addresses) {
  const map = new YMap(document.getElementById('map'));
  const geoCode = new YGeoCode();

  addresses.forEach(address => {
    geoCode.getLocations(address, function(geoPoints) {
      if (geoPoints.length > 0) {
        const point = geoPoints[0].getYGeoPoint();
        map.addMarker(point);
      }
    });
  });
}

This approach lets you convert addresses to YGeoPoints without redrawing the map. Just remember to handle potential errors and maybe add a slight delay between geocoding requests to avoid hitting rate limits. Good luck with your project!

have u tried using the geocoder service? it’s part of the yahoo maps api. u can use it to convert addresses to coordinates without redrawing the map. Here’s a basic idea:

const geocoder = new YGeo.Geocoder();
geocoder.geocode(address, (results) => {
  if (results.success) {
    const point = results.GeoPoint;
    map.addMarker(point);
  }
});

hope this helps!

While Yahoo Maps API doesn’t provide a direct method to convert addresses to coordinates, you can use the Yahoo GeoPlanet API for this purpose. It offers geocoding services that can transform addresses into lat/long coordinates.

Here’s a basic approach:

  1. Make an AJAX call to the GeoPlanet API with your address.
  2. Parse the response to extract the latitude and longitude.
  3. Create a YGeoPoint with these coordinates.
  4. Use this YGeoPoint to add your marker.

Keep in mind that you’ll need to handle API rate limits and error cases. Also, consider batch geocoding if you’re dealing with multiple addresses to improve performance.

Alternatively, if you prefer to stick with the Yahoo Maps API, you could create an off-screen map element, use drawZoomAndCenter() on it, and then retrieve the coordinates. This method, while not ideal, could work as a fallback option.