我有一個應用程序,通過谷歌地圖api(距離矩陣和地圖API)使用地理定位來計算距離並在地圖上顯示引腳。我的邏輯存在性能問題(我的數據庫中有100個地址,我全部抓住它們,將所有距離矩陣運行並獲得距離)我每次用戶點擊時都會這樣做更新位置按鈕。但是這種方法有一個問題,每次刷新按鈕被點擊時,我會發出100個請求,並且它只針對1個用戶。顯然有時候,API豐富了每天請求的限制。我的問題是:如何獲得更好的性能並改善我的邏輯,以便此應用更快運行並且不會達到每日請求限制?我有大約1000人使用這個應用程序。谷歌地圖API請求性能問題
這裏是我的代碼:
<?php
header('Content-Type: application/json');
// Open the connection with the database
$conn = mysql_connect('localhost', 'gennovacap', 'coupons');
mysql_select_db('coupons', $conn);
$id_voucher = $_POST['id_voucher'];
$uuid = $_POST['uuid'];
// $uuid = "1343C38F-C0F3-4D86-B763-C14CFBF1DAC3";
// $id_voucher = 8;
$q = mysql_query("SELECT * FROM vouchers WHERE id_voucher = ".$id_voucher);
$voucher = mysql_fetch_assoc($q);
if(preg_match('/[\+]{2}+/', $voucher['address'])) {
$i = 0;
foreach(explode('++', $voucher['address']) as $address){
$address = preg_replace('/\s/m', '+', $address);
$request = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$address."&sensor=true";
$result = simplexml_load_file($request);
$latitude = $result->result->geometry->location[0]->lat;
$longitude = $result->result->geometry->location[0]->lng;
$json['markers'][$i]['latitude'] = $latitude;
$json['markers'][$i]['longitude'] = $longitude;
$json['markers'][$i]['titlev'] = $voucher['name'];
$i++;
}
} else {
$address = preg_replace('/\s/m', '+', $voucher['address']);
$request = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$address."&sensor=true";
$result = simplexml_load_file($request);
$latitude = $result->result->geometry->location[0]->lat;
$longitude = $result->result->geometry->location[0]->lng;
$json['markers'][0]['latitude'] = $latitude[0];
$json['markers'][0]['longitude'] = $longitude[0];
$json['markers'][0]['titlev'] = $voucher['name'];
}
// See if the user redeemed this coupon
$redemption = mysql_query("SELECT * FROM redemptions WHERE uuid = '".$uuid."' AND id_voucher = '".$id_voucher."'");
$count = mysql_num_rows($redemption);
if($count > 0) {
$created_at = mysql_fetch_row($redemption);
$json['titlev'] = $voucher['name'];
$json['deal'] = $voucher['deal'];
$json['redemptions'] = false;
$json['redemptions_text'] = $created_at[3];
} else {
$json['titlev'] = $voucher['name'];
$json['deal'] = $voucher['deal'];
$json['redemptions'] = true;
$json['redemptions_text'] = "Redeem This Coupon";
}
print json_encode($json);