首先,我尝试在php中获取lat和lng值:
<?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;
}
?>
我试图在有或没有密钥的情况下调用API;浏览器密钥或服务器密钥;使用http或https,但是,这些都无法获得lat和lng(测试URL http://10.211.226.100/mobile/test_location2.php)。但是,使用相同的代码,当文件在没有任何密钥的另一个服务器(测试URL http://10.64.110.68/mobile/test_location2.php)时,我可以使用lat和lng。为什么以及如何在10.211.226.100中使其工作?
其次,虽然未能在php中获得lat和lng,但我试图在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>
我可以看到我已经获得了lat和lng,因为在地理编码功能中可以在控制台中记录这些值。但是,我在功能之外丢失了它们。
console.log(engineer_on_site_jobs)按预期输出
console.log(engineer_on_site_jobs [0])按预期输出
console.log(engineer_on_site_jobs [0] ['test_val'])输出111 instaed of the espected 444
console.log(engineer_on_site_jobs [0] ['location'])输出对象{lat:null,lng:null}
console.log(engineer_on_site_jobs [0] .location ['lat'])输出一个预期的lat值的null instaed
console.log(engineer_on_site_jobs [0] ['location'] ['lat'])输出一个预期的lat值的null instaed
console.log(engineer_on_site_jobs [0] .location.lat)输出一个预期的lat值的null instaed
这与执行时间/顺序有关吗?如何在地理编码功能中设置lat和lng值并在函数外部使用它们?