0

Im trying to send requests to the Google Elevation API, I have successfully sent singular requests. Stated in the documentation for GE API is that a singular request can contain many coordinate evaluations if the coordinates are passed as a list separated by "|".

As you can see in my code I am following the format with requests containing something like this >> 3.222,54.333 | 2.444, 66.4332 | ...

This should be working from what I can tell but clearly something is wrong.

here is a segment of the code, the program breaks at the error point within this block.

finalStringConvertedCoordinates = processedQueryPoints.joined(separator: "|")
        let apiKey = "REDACTED"
        guard let url = URL(string: "https://maps.googleapis.com/maps/api/elevation/json?key=\(apiKey)&locations=\(finalStringConvertedCoordinates)") else {
                    print("Error: cannot create URL")
                    return
DESWIJ
  • 63
  • 6
  • try to print the error message. Also, try to print finalStringConvertedCoordinates to be sure that the string is valid – Taron Qalashyan Aug 13 '21 at 09:03
  • can you show an example of "processedQueryPoints" – workingdog support Ukraine Aug 13 '21 at 10:06
  • Hi @workingdog the processedQueryPoints are like so 3.222,54.333 | 2.444, 66.4332 |... they follow that format – DESWIJ Aug 13 '21 at 10:18
  • so there is no need for joined(separator: "|") – workingdog support Ukraine Aug 13 '21 at 10:27
  • @workingdog. Apologies, no, I described this badly. The layout of processedQueryPoints is like so. [33.2343, 55.3383, 33.2323, 44.6545] It is an array of strings which is being appended to. Each pair of coordinates is at a different address within the Array – DESWIJ Aug 13 '21 at 10:36
  • that look like an array of numbers. Which is it an array of string or numbers. And what does " Each pair of coordinates is at a different address within the Array" mean? Can you give a real example? – workingdog support Ukraine Aug 13 '21 at 10:48
  • They are strings at this point, they where converted before being inserted into processedQueryPoints. in regard to Different addresses its like so [lat1,lon1,lat2,lon2,lat3,lon3] each of these represents coordinates. So, the array would look like this in terms of elements [0,1,2]. lat1,lon1 at address 0, lat2,lon2 at address 1. and so on. Just to clarify the request runs just fine until there are more than one pair of coordinates given. – DESWIJ Aug 13 '21 at 11:00
  • Can you give a real practical example of say 2 locations? – workingdog support Ukraine Aug 13 '21 at 11:06
  • I don't have enough rep to upvote you. Thank you for your help. Anyone in future experiencing this issue this is the answer to look at. Thank you very much working dog – DESWIJ Aug 13 '21 at 12:20

1 Answers1

0

try something like this, once you figure out what's in processedQueryPoints:

        let locations = ["3.222,54.333", "2.444,66.4332"]
        let locs = locations.joined(separator: "%7C")  // <-- the important bit
        print("\n---> locs: \(locs)")
        let apiKey = "REDACTED"
        let url = URL(string: "https://maps.googleapis.com/maps/api/elevation/json?locations=\(locs)&key=\(apiKey)")
        print("---> url: \(url) \n")

EDIT: starting with an array of strings as described:

        let rawData = ["3.222","54.333", "2.444","66.4332"]
        print("\n---> rawData: \(rawData)")
        
        let pairs = stride(from: 0, to: rawData.endIndex, by: 2).map {(rawData[$0], rawData[$0.advanced(by: 1)])}
        print("\n---> pairs: \(pairs)")
        
        let locs = pairs.map{ $0 + "," + $1 }.joined(separator: "%7C")
        print("\n---> locs: \(locs)")
        
        let apiKey = "REDACTED"
        let url = URL(string: "https://maps.googleapis.com/maps/api/elevation/json?locations=\(locs)&key=\(apiKey)")
        print("---> url: \(url) \n")