Search code examples
iphoneipadcloudmade

Problem with downloading tiles from cloudmade


I need download tiles for specified area. I have a bbox with lat/long coordinates, but how convert them to x/y which are required by URL


Solution

  • To work out which tile you need for a given latitude and longitude, you first need to pick a zoom level that you're interested in. Then calculate:

    n = 2 ^ zoom
    xtile = ((lon_deg + 180) / 360) * n
    ytile = (1 - (ln(tan(lat_rad) + sec(lat_rad)) / Pi)) / 2 * n
    

    Note: ln stands for natural logarithm round down xtile and ytile to get the integer tile numbers. If you need to go the other way:

    n = 2 ^ zoom
    lon_deg = xtile / n * 360.0 - 180.0
    lat_rad = arctan(sinh( Pi * (1 - 2 * ytile / n)))
    lat_deg = lat_rad * 180.0 / Pi
    

    That should be enough to get you up and running. To get help with any of CloudMade's APIs and to keep up-to-date with our latest releases, take a look at the developers mailing list.