Sometimes, the result of the longitude is 0000000. The function is not so accurate, how can i fix this? <!doctype html> <html> <head> <link href="style.css" rel="stylesheet" type="text/css"> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php include('db.php'); $bestel_id = "1"; $query = "select Adres, Postcode, Stad from Bestelling where Bestel_id = '$bestel_id'"; $result = mysql_query($query) or die (mysql_error()); while ($row = mysql_fetch_array($result)) { $plaats = "$row[Adres] $row[Postcode] $row[Stad]"; } function getLatitude($address){ $address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address"; $response = file_get_contents($url); $json = json_decode($response,TRUE); //generate array object from the response from the web return ($json['results'][0]['geometry']['location']['lat']); } echo getLatitude($plaats); function getLongitude($address){ $address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address"; $response = file_get_contents($url); $json = json_decode($response,TRUE); //generate array object from the response from the web return ($json['results'][0]['geometry']['location']['lng']); } echo getLongitude($plaats); ?> <input type="hidden" class="latitude" value="<?php echo getLatitude($plaats); ?>"> <input type="hidden" class="longtitude" value="<?php echo getLongitude($plaats); ?>"> </body> </html> <!DOCTYPE html> <html> <head> <style> #map { height: 400px; width: 100%; } </style> </head> <body> <h3>My Google Maps Demo</h3> <div id="map"></div> <script> function initMap() { var latitude = document.querySelector('.latitude').value; var longtitude = document.querySelector('.longtitude').value; var myLatlng = new google.maps.LatLng(latitude,longtitude); var map = new google.maps.Map(document.getElementById('map'), { zoom: 11, center: myLatlng }); var marker = new google.maps.Marker({ position: myLatlng, map: map }); } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB6k4w702Zoxq_QqaKubXyh6Yc62jl4ibA&callback=initMap"> </script> </body> </html>
... View more