Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alihoushyaripour's avatar

How to get user location info(state/city) from his lat lng?

Hi,

I want to get user state/city of user from his ipv4 address using google static api, but take too long...this is my code:

$lat = $v['lat'];
$lng = $v['lng'];

$json = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lng&sensor=true";

$response = json_decode(file_get_contents($json));

Is there any other faster solution?

0 likes
1 reply
realrandyallen's avatar
Level 44

If you put that URL directly into your browser you'll see that Google no longer allows you to do this without an API key:

http://maps.googleapis.com/maps/api/geocode/json?latlng=37.422290,-122.084057&sensor=true

Get an API key:

https://developers.google.com/maps/documentation/javascript/get-api-key

I think you can get a couple hundred dollars worth of api calls a month before you start getting charged. In the past I've used Bing Maps because it was still free to geocode (and I think still is):

Grab a key:

https://msdn.microsoft.com/en-us/library/ff701702.aspx

Geocode:

https://msdn.microsoft.com/en-us/library/ff701715.aspx

Maybe somebody has a better solution for something else to use nowadays.

Please or to participate in this conversation.