-2

I am trying to get latitude and longitude of a place, however, its returning different value than the value provided by google search.

        $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($request->address).",US&key=API_KEY";
        try {
            $client = new \GuzzleHttp\Client();
            $apiRequest = $client->request('GET', $url);
            $resp = json_decode($apiRequest->getBody());
        } catch (\Exception $re) {
            return response()->json([
                'sts' => -1,
                'message' => $re->getMessage()],
                500);
        }

        $latitude = $resp->results[0]->geometry->location->lat;
        $longitude = $resp->results[0]->geometry->location->lng;

So for testing purpose, I have passed New York as the address. It returns

[location] => stdClass Object
                    (
                        [lat] => 43.2994285
                        [lng] => -74.2179326
                    )

However, when I google with keyword lat long of New York it returns value

40.7128° N, 74.0060° W

I don't get why it is returning the difference of 3 degree, I understand there could be some minor difference like in this page, however, difference of 3 degree, is quite inconvenient. Is there something, I might be missing?

Aayush Dahal
  • 856
  • 1
  • 17
  • 51
  • The difference can come from one using miles and one using kilometers. Have you also set the zoom? This could have some difference also – Jayr Sep 19 '19 at 08:52
  • that miles and km could be used while calculating distance, however, upto this point I am just trying to get latitude and longitude – Aayush Dahal Sep 19 '19 at 09:15
  • That was not what you asked, you asked why there is a difference between the two – Jayr Sep 19 '19 at 09:28
  • I have'nt used zoom. Also, I have never heard about that zoom could make the difference in final latitute and longitude values. And, upto this point (getting latitude and longitue) I don't think there is any relation with neither km nor miles. – Aayush Dahal Sep 19 '19 at 09:53
  • I think is because "New York" is not just a city, is also an state. So depending on wich kind of search or properties you use, it will return one thing or another. Or maybe both. It returns you a single result or more than one?. – Mikel Sep 19 '19 at 13:28

1 Answers1

0

I can see that you have geocoded the address "New York, US" in which the geocoder sees as an administrative area or a state. Please see the result in the geocoder tool here which shows the approximate location 43.299429,-74.217933 for the New York State.

On the other hand, if you geocode the address "New York, NY, USA", you should get the approximate location 40.712775,-74.005973 which is for New York City which is a locality.

Hope this clarifies it.

Angelica
  • 687
  • 4
  • 16