Location-based search is a widely used and practical feature in modern web applications, helping users quickly find nearby businesses, attractions, or other points of interest. This article walks you through the process of implementing accurate and efficient location-based search using PHP and Xunsearch.
First, install Xunsearch on your server environment. Download the latest installation package from the official Xunsearch channels and follow the official instructions to complete the installation and start the service.
Assume we have a database table named location that stores business names along with their longitude and latitude coordinates. Use the following SQL statement to create the table structure:
CREATE TABLE location ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, longitude DOUBLE NOT NULL, latitude DOUBLE NOT NULL );
Insert sample data as follows:
INSERT INTO location (name, longitude, latitude) VALUES ('Business 1', 121.47856, 31.23542), ('Business 2', 121.52142, 31.22584), ('Business 3', 121.51993, 31.15673), ('Business 4', 121.45218, 31.24736), ('Business 5', 121.52306, 31.19175);
Use the PHP SDK provided by Xunsearch to create an index and specify the fields for search. Example code is as follows:
<?php // Include Xunsearch library require_once '/path/to/xunsearch/sdk/php/lib/XS.php'; // Create Xunsearch object $xs = new XS('location'); // Get the index object $index = $xs->index; // Begin rebuilding the index $index->beginRebuild(); // Set the database table $index->setDB($xs->db->getDb('location')); // Define index fields and types $index->setScheme(new XSFieldScheme([ 'name' => 'string', 'longitude' => 'numeric', 'latitude' => 'numeric' ])); // Finish rebuilding the index $index->endRebuild(); ?>
Write a PHP script to receive longitude and latitude parameters from the user, then perform a search using Xunsearch's search capabilities to return a list of nearby businesses. Example code:
<?php // Include Xunsearch library require_once '/path/to/xunsearch/sdk/php/lib/XS.php'; // Get longitude and latitude parameters $longitude = $_GET['longitude']; $latitude = $_GET['latitude']; // Create Xunsearch object $xs = new XS('location'); // Get search object $search = $xs->search; // Enable fuzzy search $search->setFuzzy(true); // Set search query $search->setQuery("longitude:$longitude AND latitude:$latitude"); // Execute search $result = $search->search(); // Output results foreach ($result as $item) { echo $item->name . '<br>'; } ?>
Test the location-based search feature by visiting the following URL:
http://yourdomain.com/search.php?longitude=121.50000&latitude=31.20000
This request will return the list of businesses nearest to the specified coordinates.
This article introduced how to implement a location-based search feature using PHP and Xunsearch, covering installation, data preparation, index building, and search implementation. Mastering these steps allows you to quickly add efficient and accurate geolocation search functionality to your web application.