https://developers.google.com/maps/articles/phpsqlsearch_v3
我正在使用Google的教程来创建商店定位器。我已经完成了商店定位器的建设,但是经过测试后,当我搜索一个半径为25英里的商店时,它会完全进入美国各地的商店,这绝对超过了25英里。有没有人遇到过此错误和/或有关如何解决此问题的建议?
例如,请看这张照片。拉链来自CT,但是从CA进入商店。的javascript:
function searchStores(lat, lng) {
var url = '/store-finder/V3/includes/store-ajax.php';
var radius = $('#radiusSelect').val();
var prod = $('#myproducts').val();
//console.log("inside searchStores radius: " + radius);
//console.log("inside searchStores Product: " + prod);
var parameter = { lat: lat, lng: lng, radius: radius, prod: prod };
jQuery.ajax({
url: url,
data: parameter,
dataType: 'json',
success: showStores,
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
//console.log(parameter);
}
PHP:
$query = sprintf("SELECT
store_id,
store_name,
address1,
city,
state,
zip,
phone,
product_type.product_type_id,
coord_lat,
coord_long,
( 3959 * acos( cos( radians('%s') ) * cos( radians( coord_lat ) ) * cos( radians( coord_long ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( coord_lat ) ) ) ) AS distance
FROM
stores, stores_product_type_link, product_type, stores_store_type_link, store_type
WHERE
stores_product_type_link.csid = stores.csid
AND (stores_product_type_link.product_type_id = '%s' OR stores_product_type_link.product_type_id = '%s')
AND stores_product_type_link.product_type_id = product_type.product_type_id
AND stores_store_type_link.csid = stores.csid
AND stores_store_type_link.store_type_id = store_type.store_type_id
HAVING
distance < '%s'
ORDER BY
distance",
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($lat),
mysql_real_escape_string($product),
mysql_real_escape_string($product2),
mysql_real_escape_string($radius));
$querystring = "SELECT
store_id,
store_name,
address1,
city,
state,
zip,
phone,
product_type.product_type_id,
coord_lat,
coord_long,
( 3959 * acos( cos( radians('$lat') ) * cos( radians( coord_lat ) ) * cos( radians( coord_long ) - radians('$lng') ) + sin( radians('$lat') ) * sin( radians( coord_lat ) ) ) ) AS distance
FROM
stores, stores_product_type_link, product_type, stores_store_type_link, store_type
WHERE
stores_product_type_link.csid = stores.csid
AND (stores_product_type_link.product_type_id = '$product' OR stores_product_type_link.product_type_id = '$product2')
AND stores_product_type_link.product_type_id = product_type.product_type_id
AND stores_store_type_link.csid = stores.csid
AND stores_store_type_link.store_type_id = store_type.store_type_id
HAVING
distance < '$radius'
ORDER BY
distance";
$result = mysql_query($query);
if (! $result)
die_with_error(mysql_error());
$result_array = array();
while ($row = mysql_fetch_assoc($result)) {
$address2 = $row['city'] . ", " . $row['state'] . " " . $row['zip'];
array_push($result_array, array(
"id" => $row['store_id'],
"name" => $row['store_name'],
"address1" => $row['address1'],
"address2" => $address2,
"phone" => $row['phone'],
"product_type_id" => $row['product_type_id'],
"lat" => $row['coord_lat'],
"lng" => $row['coord_long'],
"distance" => round($row['distance'], 2),
"qry" => $querystring
));
}
表单输入:
<label class="radius" style="display:none"><h3>Radius:</h3></label>
<select id="radiusSelect">
<option value="5" selected>5mi</option>
<option value="10">10mi</option>
<option value="25">25mi</option>
</select>