1

I am learning Xamarin forms. I am using Google Place API for searching nearby

I have managed to call the API in .NET, but I only get 20 results as it is written in the Google document.

Here is my code :

string lati = location.Latitude.ToString().Replace(",", ".");
string longi = location.Longitude.ToString().Replace(",", ".");

string latitude = lati;
string longitude = longi;
string Myradius = "3000";

var URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
string urlParameters = "?location=" + latitude + "," + longitude + "&radius=" + Myradius + "&key=My_GOOGLE_KEY";

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);

// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

// List data response.
HttpResponseMessage response = await client.GetAsync(urlParameters);

if (response.IsSuccessStatusCode)
{
    JObject joResponse = JObject.Parse(await response.Content.ReadAsStringAsync());

    JArray MyJsonResult = (JArray)joResponse["results"];

    // I want to have the second page result of my API Call
    string token = joResponse["next_page_token"].ToString(); // I have manage to get the token
    JArray MyJsonResult2 = await GetnextPageData(latitude, longitude, Myradius, token); //no result
}

async Task<JArray> GetnextPageData(string latitude, string longitude, string Myradius , string token)
{
    var URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?";

    string urlParameters = "?location=" + latitude + "," + longitude + "&radius=" + Myradius + "&key=My_GOOGLE_KEY&pagetoken=" + token;

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(URL);

    // Add an Accept header for JSON format.
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // List data response
    HttpResponseMessage response = await client.GetAsync(urlParameters);
    Console.WriteLine("MyPrint " + response);

    if (response.IsSuccessStatusCode)
    {
        JObject joResponse = JObject.Parse(await response.Content.ReadAsStringAsync());
        Console.WriteLine("MyPrint " + response);

        JArray MyJsonResult = (JArray)joResponse["results"];

        if (MyJsonResult != null)
        {
            return MyJsonResult;
        }
        else
        {
            return null;
        }
    }

    return null;
}

I have found those link but still cannot add the parameter for the rest of the result:

Obtaining more than 20 results with Google Places API

How to get 20+ result from Google Places API?

Problem: I have manage to get the token but when I put it in the parameter I get no result - MyJsonResult2 is empty.

When I print "response" for the second call I have this :

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.NSUrlSessionHandler+NSUrlSessionDataTaskStreamContent, Headers:
{
  server-timing: gfet4t7; dur=30
  Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
  Server: scaffolding
  Server: on
  Server: HTTPServer2
  x-xss-protection: 0
  Cache-Control: public, max-age=300
  Date: Sun, 13 Nov 2022 02:19:16 GMT
  X-Frame-Options: SAMEORIGIN
  Vary: Accept-Language
  Content-Type: application/json; charset=UTF-8
  server-timing: gfet4t7; dur=30
  Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
  Content-Encoding: gzip
  x-xss-protection: 0
  Expires: Sun, 13 Nov 2022 02:24:16 GMT
  Content-Length: 86
  X-Frame-Options: SAMEORIGIN
}

And when I print "joResponse" for the second call I have this :

{
  "html_attributions": [],
  "results": [],
  "status": "INVALID_REQUEST"
}

Thanks for your help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
hugokis
  • 147
  • 2
  • 14
  • 1
    The 2nd answer in the final link in your question shows **exactly** how to do this. Where exactly are you stuck? – Jason Nov 13 '22 at 00:07
  • 1
    You have to call the API **three** times. Each call returns 20 results. For the 2nd and 3rd calls you have to pass the token value from the previous response – Jason Nov 13 '22 at 00:11
  • @Jason I have managed to get the token but when I add it in url I have not result : string urlParameters = "?location=" + latitude + "," + longitude + "&radius=" + Myradius + "&key=GOOGLE_KEY&pagetoken=" + token; – hugokis Nov 13 '22 at 01:32
  • Please [edit] your question to include the actual code. And what specific http result code or json do you get from the 2nd request? – Jason Nov 13 '22 at 01:43
  • I trying to save the result of my second page in MyJsonResult2 but it is empty when I do the API call with the token – hugokis Nov 13 '22 at 01:55
  • What is the response code of the 2nd request? And what are the contents of the response? – Jason Nov 13 '22 at 02:07
  • @Jason I have INVALID_REQUEST when I print joResponse. I have updated the code – hugokis Nov 13 '22 at 02:24
  • @Jason the documentation says to use pagetoken I do not know why the request is not valid – hugokis Nov 13 '22 at 02:34
  • @Jason I have found the solution and post the answer – hugokis Nov 13 '22 at 12:42

1 Answers1

0

I have found the solution. I must add in my request the parameters :"hasNextPage=true&nextPage()=true"

hugokis
  • 147
  • 2
  • 14
  • Might be nice to see a little more of the solution here. Add the code to show the call being made where the parameters were missing then and updated sample with the correct parameters. – Montané Hamilton Nov 15 '22 at 23:49
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 15 '22 at 23:50