How to get latitude and longitude from address in php without api key

I have function that get address and return latitude and longitude.

The problem is that after a while i'm getting 0.00000 for both lat & lng. I guess that i need to add "&key=API_KEY" to the url, and if i got it right it's cost money.

Is there any other way to get address lat & lng ?

function get_address_lat_lng ($address)
{

    $prepAddr = str_replace(' ','+',$address);

    $geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.urlencode($prepAddr).'&sensor=false');

    $output= json_decode($geocode);

    $lat = $output->results[0]->geometry->location->lat;
    $lng = $output->results[0]->geometry->location->lng;

    return (array('lat' => $lat, 'lng' => $lng) );
}

asked Sep 2, 2018 at 11:37

3

function ipLookUp () {
  $.ajax('http://ip-api.com/json')
   .then(
    function success(response) {
      console.log('User\'s Location Data is ', response);
      console.log('User\'s Country ', response.country);
    },

    function fail(data, status) {
      console.log('Request failed.  Returned status of ', status);
    }
  );
}
ipLookUp()

Check your console logs on browsers but this is based on IP of client.

How to get latitude and longitude from address in php without api key

answered Sep 2, 2018 at 11:43

How to get latitude and longitude from address in php without api key

4

How to get latitude and longitude from address in php without api key

Demo

Google maps can be embedded in your website using PHP without providing API key. You just need to enter your address. Or you can enter coordinates, longiude and latitude.

Google Maps is a web mapping service developed by Google. It offers satellite imagery, aerial photography, street maps, 360° panoramic views of streets, real-time traffic conditions. Also, route planning for traveling by foot, car, bicycle and air, or public transportation.

By using the Google Maps API, it is possible to embed Google Maps into an external website, on to which site-specific data can be overlaid. Although initially only a JavaScript API, the Maps API was expanded to include an API for Adobe Flash applications. But this has been deprecated. Adobe Flash was a service for retrieving static map images, and web services for performing geocoding, generating driving directions, and obtaining elevation profiles. Over 1,000,000 web sites use the Google Maps API, making it the most heavily used web application development API. In September 2011, Google announced it would deprecate the Google Maps API for Flash.

As of 21 June 2018, Google increased the prices of the Maps API and requires a billing profile. But some people didn’t find themselve comfortable with providing their credit card information. Or they are just students who want to use Google Map services. So by following the below tutorial you will be able to show Google Map in your website without providing API key. You just need to enter either address or co-ordinates like latitude and longitude.

Create a simple form that will have an input field for entering address via input field. And a submit button which when clicked will submit the form.

<form method="POST">
	<p>
		<input type="text" name="address" placeholder="Enter address">
	</p>

	<input type="submit" name="submit_address">
</form>

Now we need to create a PHP block that will be executed when the form submits. In that block, we will get the address entered by user in a separate variable. Since address might have spaces so we will be converting those spaces into + sign. Lastly, we will create an iFrame tag to render the map. &output=embed is important since it tells the Google Map’s server that we are going to embed the map in an iFrame.

<?php
	if (isset($_POST["submit_address"]))
	{
		$address = $_POST["address"];
		$address = str_replace(" ", "+", $address);
		?>

		<iframe width="100%" height="500" src="https://maps.google.com/maps?q=<?php echo $address; ?>&output=embed"></iframe>

		<?php
	}
?>

Google maps by latitude and longitude without API key

Create a simple form that will have 2 input fields for entering latitude and longitude via input field. And a submit button which when clicked will submit the form.

<form method="POST">
	<p>
		<input type="text" name="latitude" placeholder="Enter latitude">
	</p>

	<p>
		<input type="text" name="longitude" placeholder="Enter longitude">
	</p>

	<input type="submit" name="submit_coordinates">
</form>

Now we need to create a PHP block that will be executed when the form submits. In that block, we will get the latitude and longitude co-ordinates entered by user in separate variables. Lastly, we will create an iFrame tag and pass those co-ordinates to render the map. Make sure to separate the values using comma (,).

<?php
	if (isset($_POST["submit_coordinates"]))
	{
		$latitude = $_POST["latitude"];
		$longitude = $_POST["longitude"];
		?>

		<iframe width="100%" height="500" src="https://maps.google.com/maps?q=<?php echo $latitude; ?>,<?php echo $longitude; ?>&output=embed"></iframe>

		<?php
	}
?>

Conclusion

Although it has some limitations, for example you cannot update the marker once the map is fully loaded. In order to change the location of marker, you can re-draw the map again. But it will give you enough functionality so that if you are receiving latitude and longitude of user’s location from some android/iOS application and you wanted to show that location on admin panel. You can easily do that by following this tutorial. That’s all for now, if you face any problem or need any help feel free to ask in the comments section below.

Post navigation

Post Views: 8,862

How to get latitude and longitude from address in php without api key

Computer Programmer. Full-stack developer. Specialized in web development (PHP), android native development (Java and Kotlin), iOS native development (Swift 4 and Swift UI), Vue JS, and Node JS and Mongo DB development. View all posts by adnanafzal565

How can I get latitude and longitude from address in PHP?

Function Usage: php $address = 'Brooklyn, NY, USA'; $address = str_replace(' ', '+', $address); $result = getGeoCode($address); echo 'Latitude: ' . $result['lat'] . ', Longitude: ' . $result['lng']; // produces output // Latitude: 40.6781784, Longitude: -73.9441579 ?>

How do I use Google Maps without API key?

Create Responsive Google Maps without API Key.
Go to Google Maps..
Enter the business address in the search bar, then click the search button..
Click on the Share icon..
Choose "Embed map".
Select the size of the map from the drop down..
Select and copy the HTML iframe embed code..

How get lat long from address Google Maps API PHP?

function getaddress($lat, $lng) { $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . trim($lat) . ',' .

How do I find the latitude and longitude of a URL?

First, open Google Maps in your browser and type “Space Needle” in the search bar. After the page finishes loading, look for coordinates in the URL. The coordinates are located behind the “@” sign.