First I tried to get the lat and lng values in php:
<?php
// set up an array that contains postcodes and the lat and lng data
$engineer_on_site_jobs=array();
$record_id = 872743;
$post_code = 'PO12 1QB';
prepare_postcode($post_code);
$on_site_time = "12:49:29";
$location = get_lat_and_lng_by_postcode($post_code);
$test_val = 111;
$engineer_on_site_jobs[] = array('record_id'=>$record_id, 'location'=>$location, 'on_site_time'=>$on_site_time, 'postcode'=>$post_code, 'test_val'=>$test_val);
$record_id = 873752;
$post_code = 'PO9 3DX';
prepare_postcode($post_code);
$on_site_time = "10:43:22";
$location = get_lat_and_lng_by_postcode($post_code);
$test_val = 222;
$engineer_on_site_jobs[] = array('record_id'=>$record_id, 'location'=>$location, 'on_site_time'=>$on_site_time, 'postcode'=>$post_code, 'test_val'=>$test_val);
echo "<pre>";
print_r($engineer_on_site_jobs);
echo "<pre>";
// local functions /////////////////////
/*
* prepare the postcode: remove space
* @param $postcode
*/
function prepare_postcode(&$post_code){
$post_code_pieces=explode(' ', $post_code);
$post_code="$post_code_pieces[0]"."$post_code_pieces[1]";
}
/*
* get loction lat and lng by postcode
* @param $postcode
* @return array &location[lat, lng]
*/
function get_lat_and_lng_by_postcode($postcode){
$loc=array();
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$postcode";
//$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$postcode&key=browser_key_string_goes_in_here";
//$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$postcode&key=server_key_string_goes_in_here";
//$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$postcode&key=browser_key_string_goes_in_here";
//$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$postcode&key=server_key_string_goes_in_here";
$json_data = @file_get_contents($url);
$my_data = json_decode($json_data, true);
$location=$my_data['results'][0]['geometry']['location'];
$loc['lat']= $location['lat'];
$loc['lng']= $location['lng'];
return $loc;
}
?>
I have tried to called the API with or without a key; a browser key or a server key; use http or https, but, these all failed to get the lat and lng (the test URL http://10.211.226.100/mobile/test_location2.php). However, with the same code, I can have the lat and lng when the file is in another server without any key (the test URL http://10.64.110.68/mobile/test_location2.php). Why and how can I make it work in 10.211.226.100 ?
Secondly, while failed to get the lat and lng in php, I tried to get them in javascript.
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
function initialize() {
var engineer_on_site_jobs = <?php echo json_encode($engineer_on_site_jobs);?>;
var geocoder = new google.maps.Geocoder();
$(engineer_on_site_jobs).each(function(i, val){
var postcode = val.postcode;
geocoder.geocode({'address': postcode}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location.lat());
console.log(results[0].geometry.location.lng());
val.location['lat'] = results[0].geometry.location.lat();
val.location['lng'] = results[0].geometry.location.lng();
val['test_val']= 444;
engineer_on_site_jobs[i] = val;
}
});
});
console.log(engineer_on_site_jobs);
console.log(engineer_on_site_jobs[0]);
console.log(engineer_on_site_jobs[0]['test_val']);
console.log(engineer_on_site_jobs[0]['location']);
console.log(engineer_on_site_jobs[0].location['lat']);
console.log(engineer_on_site_jobs[0]['location']['lat']);
console.log(engineer_on_site_jobs[0].location.lat);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
I can see that I have got the lat and lng as the values can be logged in the console when within the geocode function. However, I have lost them outside the function.
console.log(engineer_on_site_jobs) output as expected
console.log(engineer_on_site_jobs[0]) output as expected
console.log(engineer_on_site_jobs[0]['test_val']) output 111 instaed of the ecpected 444
console.log(engineer_on_site_jobs[0]['location']) output Object {lat: null, lng: null}
console.log(engineer_on_site_jobs[0].location['lat']) output null instaed of an ecpected lat value
console.log(engineer_on_site_jobs[0]['location']['lat']) output null instaed of an ecpected lat value
console.log(engineer_on_site_jobs[0].location.lat) output null instaed of an ecpected lat value
Does this related to execution timing/order? How can I set the lat and lng values inside the geocode function and use them outsite the function?