0

I'm trying to serve requests to Google API using a JavaFX app. I'm using the Google roads API. Problem is I'm asking user to import an excel document with coordinates and the document can hold as many latitude and longitude data as possible but the Google API only allows less than 100 pairs of coordinates. So how can I serve the data which is in an array list from index at position 0 to 99 and on button press serve the next set of coordinates from 100 to 199 or less. I'm currently able to serve the arraylist.sublist(0to99) and get back a json response. Thanks in advance

5
  • First arraylist.sublist(0,99) make the api request and save the date, and then arraylist.sublist(100,199) make another api request. Commented Jul 23, 2015 at 17:51
  • That helps. But I get an exception error because the data is not necessarily extending to the 199 pair of coordinates. In my case, the test excel file has 154 pairs of coordinates. So arraylist.sublist(0to99) is valid but. Sublist(100to199) doesn't have data the whole way hence the exception. So how do I make it intelligent? Because the coordinates can be as little as two and as many as the users imported excel file. Commented Jul 23, 2015 at 18:39
  • Maybe you should try something like .size() method such that arraylist.sublist(100, arraylist.size()). Commented Jul 23, 2015 at 18:43
  • Works perfectly but there are some scenarios not dealt with. Like if the the excel file has two, in that case arraylist.sublist(0, arraylist.size()) works. And that works for data less than 100 pairs of coordinates too. But how can I serve the next coordinates if the data has for example 309 coordinates. Doing arraylist.sublist(0,arraylist.size()) will give me a json response that I have exceeded the limit of parameters in the url, and doing arraylist.sublist(0,99) only caters for part of the imported coordinates data from the excel file. Commented Jul 23, 2015 at 19:05
  • You need a little bit algorism. Take look at here and here. Commented Jul 23, 2015 at 19:13

1 Answer 1

0
    //On fx button click the following happens
    @FXML public void loadURL(Event event){ 
    co_ordinates = Excel_Exchange.value;
    if(!next){
        limit = (int)co_ordinates.size();
        next = true;//some global boolean variable so that this is done once
    }
    if(co_ordinates.size()<100){
        StringBuilder urlCaseOne = new StringBuilder(co_ordinates.subList(start, co_ordinates.size()).toString().replaceAll("[\\[\\]]","").replaceAll(", ",""));
        url_link = "https://roads.googleapis.com/v1/snapToRoads?path="+urlCaseOne.deleteCharAt(urlCaseOne.length()-1)+"&interpolate=true&key="+API_KEY;
    }else{
        if(limit>100){
            StringBuilder urlCaseTwo = new StringBuilder(co_ordinates.subList(start, end).toString().replaceAll("[\\[\\]]","").replaceAll(", ",""));
            url_link = "https://roads.googleapis.com/v1/snapToRoads?path="+urlCaseTwo.deleteCharAt(urlCaseTwo.length()-1)+"&interpolate=true&key="+API_KEY;
            //System.out.println("l"+limit+" s"+start+" e"+end);
            start+=100; end+=100; limit-=100;
        }else if(limit<100){
            StringBuilder urlCaseThree = new StringBuilder(co_ordinates.subList(start, co_ordinates.size()).toString().replaceAll("[\\[\\]]","").replaceAll(", ",""));
            url_link = "https://roads.googleapis.com/v1/snapToRoads?path="+urlCaseThree.deleteCharAt(urlCaseThree.length()-1)+"&interpolate=true&key="+API_KEY;
        }
    }
    //System.out.println(co_ordinates.size());
    //System.out.println(url_link);
    //System.out.println(co_ordinates.toString().lastIndexOf("|"));
    //System.out.println(co_ordinates.subList(0, 99).size());
    startLoadState.apply();
    this.engine.load(url_link);
}// i have another method that navigates back by to the first url
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.