在Laravel中使用Google地图和MySQL进行半径搜索,可以通过以下步骤实现:
GOOGLE_MAPS_API_KEY=your_api_key
composer require guzzlehttp/guzzle
MapController
,并在其中编写以下代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class MapController extends Controller
{
public function search(Request $request)
{
$address = $request->input('address');
$radius = $request->input('radius');
// 使用Google Geocoding API将地址转换为经纬度坐标
$client = new Client();
$response = $client->get('https://maps.googleapis.com/maps/api/geocode/json', [
'query' => [
'address' => $address,
'key' => config('app.google_maps_api_key'),
],
]);
$data = json_decode($response->getBody(), true);
$location = $data['results'][0]['geometry']['location'];
$latitude = $location['lat'];
$longitude = $location['lng'];
// 使用MySQL进行半径搜索
$results = DB::select(DB::raw('
SELECT *,
(6371 * acos(cos(radians(' . $latitude . ')) * cos(radians(latitude)) * cos(radians(longitude) - radians(' . $longitude . ')) + sin(radians(' . $latitude . ')) * sin(radians(latitude))))
AS distance
FROM locations
HAVING distance < ' . $radius . '
ORDER BY distance
'));
return response()->json($results);
}
}
请注意,上述代码中的locations
是你的MySQL数据库中存储位置信息的表名。
MapController
的search
方法:
Route::post('/search', 'MapController@search');
search.blade.php
:
<html>
<head>
<title>Radius Search</title>
<script src="https://maps.googleapis.com/maps/api/js?key={{ config('app.google_maps_api_key') }}&libraries=places"></script>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
var input = document.getElementById('address');
var searchBox = new google.maps.places.SearchBox(input);
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length === 0) {
return;
}
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
markers.push(new google.maps.Marker({
map: map,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
</script>
</head>
<body>
<input id="address" type="text" placeholder="Enter a location">
<input id="radius" type="number" placeholder="Enter a radius">
<button onclick="search()">Search</button>
<div id="map" style="height: 400px;"></div>
<script>
function search() {
var address = document.getElementById('address').value;
var radius = document.getElementById('radius').value;
axios.post('/search', {
address: address,
radius: radius
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
}
</script>
</body>
</html>
请注意,上述代码中使用了Axios库来发送异步POST请求,确保你已经在项目中安装了Axios。
/search
路由,你将看到一个包含Google地图和搜索表单的页面。输入地址和半径后,点击搜索按钮,将会在控制台中打印出符合条件的结果。这是一个基本的示例,你可以根据自己的需求进行修改和扩展。在这个示例中,我们使用了Google Maps JavaScript API和MySQL来实现在Laravel中使用Google地图和MySQL进行半径搜索。
领取专属 10元无门槛券
手把手带您无忧上云