0

this is my coding

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Live Demo of Google Maps Geocoding Example with PHP</title>
</head>
<?php
if($_POST){

// get latitude, longitude and formatted address
$data_arr = geocode($_POST['address']);

// if able to geocode the address
if($data_arr){

    $latitude = $data_arr[0];
    $longitude = $data_arr[1];
    $formatted_address = $data_arr[2];

    ?>

    <!-- google map will be shown here -->
    <div id="gmap_canvas">Loading map...</div>
    <div id='map-label'>Map shows approximate location.</div>

    <!-- JavaScript to show google map -->
    <script type="text/javascript" src="https://maps.google.com/maps/api/js?key=AIzaSyDTcfk6x2LlTTtqMesz2G1IolPW0P84Q7k&callback"></script>
    <script type="text/javascript">
        function init_map() {
            var myOptions = {
                zoom: 14,
                center: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);
            marker = new google.maps.Marker({
                map: map,
                position: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>)
            });
            infowindow = new google.maps.InfoWindow({
                content: "<?php echo $formatted_address; ?>"
            });
            google.maps.event.addListener(marker, "click", function () {
                infowindow.open(map, marker);
            });
            infowindow.open(map, marker);
        }
        google.maps.event.addDomListener(window, 'load', init_map);
    </script>

    <?php

    // if unable to geocode the address
}else{
    echo "No map found.";
}
}
?>
<body>
<form action="" method="post">
    <input type='text' name='address' placeholder='Enter any address here' />
    <input type='submit' value='Geocode!' />
</form>
<?php
// function to geocode address, it will return false if unable to geocode address
function geocode($address){

// url encode the address
$address = urlencode($address);

// google map geocode api url
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=AIzaSyDTcfk6x2LlTTtqMesz2G1IolPW0P84Q7k&callback";

// get the json response
$resp_json = file_get_contents($url);

// decode the json
$resp = json_decode($resp_json, true);

// response status will be 'OK', if able to geocode given address
if($resp['status']=='OK'){

    // get the important data
    $lati = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] : "";
    $longi = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : "";
    $formatted_address = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] : "";

    // verify if data is complete
    if($lati && $longi && $formatted_address){

        // put the data in the array
        $data_arr = array();

        array_push(
            $data_arr,
            $lati,
            $longi,
            $formatted_address
        );

        return $data_arr;

    }else{
        return false;
    }

}

else{
    echo "<strong>ERROR: {$resp['status']}</strong>";
    return false;
}
}
?>
</body>
</html>

i got this code from this website https://www.codeofaninja.com/2014/06/google-maps-geocoding-example-php.html and this is my website that show this code http://econmarket.000webhostapp.com/tracking.php idk how to fix it but this is all i got, everytime i input address in the textbox it always show the error idk why.

2 Answers2

0

If I enter your request into my browser (with an arbitrary address):

https://maps.googleapis.com/maps/api/geocode/json?address=Nebraska&key=AIzaSyDTcfk6x2LlTTtqMesz2G1IolPW0P84Q7k&callback

I get:

{ "error_message" : "This API project is not authorized to use this API. Please ensure this API is activated in the Google Developers Console: https://console.developers.google.com/apis/api/geocoding_backend?project=_", "results" : [], "status" : "REQUEST_DENIED" }

Authorize the Google Maps Geocoder for that key (or better, disable or delete that key, as you disclosed it publicly, and create a new one restricted to your site that has the Geocoding API authorized)

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • i have fixed my api but after i've search it still shows the same error – Eric Anthony Wu Apr 11 '18 at 14:01
  • What do you mean by you fixed your api? By the "same error" do you mean the error message in my answer or the "No map found." that comes from your code? What address are you using in the request? – geocodezip Apr 11 '18 at 15:21
-1

Did you specify the sensor parameter on the request?

"REQUEST_DENIED" indicates that your request was denied, generally because of lack of a sensor parameter.

sensor (required) — Indicates whether or not the geocoding request comes from a device with a location sensor. This value must be either true or false

Add this in the request : &sensor=true

Léo R.
  • 2,620
  • 1
  • 10
  • 22
  • where was it? in the variabel url? @Leo R. – Eric Anthony Wu Apr 11 '18 at 13:10
  • The sensor parameter is [no longer required](https://stackoverflow.com/questions/3212550/google-geocoding-api-request-denied), please provide a reference for the quoted text in your answer. – geocodezip Apr 11 '18 at 13:13
  • Where did you get the quoted text in your answer from? Please provide a link to the official documentation stating that (as to the best of my knowledge that is not correct). – geocodezip Apr 11 '18 at 13:16
  • @geocodezip https://www.codeofaninja.com/2014/06/google-maps-geocoding-example-php.html this is where i get my code from – Eric Anthony Wu Apr 11 '18 at 13:22
  • This answer is longer correct see: [Google Geocoding API - REQUEST_DENIED](https://stackoverflow.com/questions/3212550/google-geocoding-api-request-denied) – geocodezip Apr 11 '18 at 13:23
  • yea i've put sensor too but it still error @geocodezip – Eric Anthony Wu Apr 11 '18 at 13:43