Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
IMPORTANT: MapPoint Web Service was retired on November 18, 2011. Please see Bing Maps for a list of current Bing Maps APIs.
Justin Southall
Microsoft Corporation
January 2004
Applies to:
Microsoft® MapPoint® Web Service
Microsoft SQL Server™ 2000
Summary: Learn how to create a Microsoft SQL Server 2000 stored procedure that runs proximity searches based on latitude and longitude coordinates. You can then incorporate this stored procedure into your MapPoint Web Service application. (8 printed pages)
Introduction
Requirements
How a Proximity Search Works
Formatting the Data
Importing the Data into SQL Server
Creating the Stored Procedure
Creating the User Login
Conclusion
Microsoft® MapPoint® Web Service provides an effective proximity search solution for all categories of location-based customer data. You load data and icons into a secure hosted environment and then access them through a powerful and flexible Web service that exposes an industry standard Simple Object Access Protocol (SOAP) API. This makes it simple for an organization to create find the nearest applications, such as store or ATM locators.
While the MapPoint Web Service proximity search solution is effective for most scenarios, in some circumstances implementing proximity searches that are independent of MapPoint Web Service may be beneficial or necessary. Three factors might lead you to want to store and query your MapPoint Web Service data locally:
This article describes how to implement proximity searches locally using a SQL Server 2000 stored procedure. Details about implementing a stored procedure in your application are beyond the scope of this document.
This section describes the software that you need and outlines requirements for your data and your SQL Server configuration.
You'll need the following software:
The procedures in this article assume the following:
A proximity search works by applying the law of cosines, which calculates the distance between two points on the globe. Because the law of cosines takes into account the curvature of the earth, it is considered better than other methods, such as the Pythagorean theorem.
An abridged version of the law of cosines is as follows:
a = sin(lat1)*sin(lat2)
b = cos(lat1)*cos(lat2)*cos(lon2-lon1)
c = arcos(a+b)
d = R*c
where lat equals latitude, lon equals longitude, R equals the Earth's radius, and d equals distance. Latitude and longitude are expressed in radians.
As you can see, the law of cosines takes two latitude and longitude pairs, one for a location that is known in advance, such as the address of a store, and one for a location that is provided by a user, such as the user's address. A third input, the radius in which to search, is also required.
During a proximity search, the distance is calculated between the user's address and each business location within the search radius, and the results are then ordered by distance.
Implementing a stored procedure for proximity searches includes the following steps:
The following sections describe each step in detail.
As mentioned earlier, you will be importing data from Access into SQL Server, and your file must meet the requirements listed on the CSS. Additionally, you'll add three fields to the Access table containing your data, and then you'll need to run three queries on those fields.
The queries populate the new fields using a portion of the proximity search algorithm, which performs the calculations for the locations that are known in advance, such as business addresses. This step could have been included in the stored procedure, but then the calculations would run every time a search is conducted. To improve performance, you run the queries run once, before you load the data into SQL Server.
The Access queries are based on the following calculations:
XAxis = cos(radians(Latitude)) * cos(radians(Longitude))
YAxis = cos(radians(Latitude)) * sin(radians(Longitude))
ZAxis = sin(radians(Latitude))
To format the Access data
Add the following fields:
Name | Type |
---|---|
XAxis | Text |
YAxis | Text |
ZAxis | Text |
Build the following three queries in Access. In the queries, [TABLENAME] represents the name of the table in which your data is stored. The following formulas are used in the queries to represent radians and PI:
UPDATE [TABLENAME] SET XAxis = (cos(((4*((4*atn(1/5))-
(atn(1/239))))/180)*[TABLENAME].Latitude)*cos(((4*((4*atn(1/5))-
(atn(1/239))))/180)*[TABLENAME].Longitude));
UPDATE [TABLENAME] SET YAxis = (cos(((4*((4*atn(1/5))-
(atn(1/239))))/180)*[TABLENAME].Latitude)*sin(((4*((4*atn(1/5))-
(atn(1/239))))/180)*[TABLENAME].Longitude));
UPDATE [TABLENAME] SET ZAxis = (sin(((4*((4*atn(1/5))-
(atn(1/239))))/180)*[TABLENAME].Latitude));
Run the queries.
When the queries run, the XAxis, YAxis, and ZAxis fields are populated. The data is now ready to be loaded into SQL Server
In this step, you use the Data Transformation Services Import/Export Wizard to import your Access data into SQL Server.
To import data
Open Enterprise Manager: On the Start menu, point to All Programs, point to Microsoft SQL Server, and then click Enterprise Manager.
In the left pane, expand Microsoft SQL Servers, expand SQL Server Group, and then expand the group containing the instance of SQL Server that you want to use.
If the instance of SQL Server is on your local computer, expand the Local group. If you want to use an instance of SQL Server on a different computer, you must add that server to the SQL Server group. For more information, see SQL Server Books Online.
Right-click Databases, point to All Tasks, and then click Import Data. When the first screen of the Data Transformation Services Import/Export Wizard appears, click Next.
On the next screen, in the Data Source drop-down list, select Microsoft Access, specify a file name for the Access database, and then click Next.
In the Destination drop-down list, ensure that Microsoft OLE DB Provider for SQL Server is selected, and in the Server drop-down list, choose the instance of SQL Server to which you want to connect.
Select either Use Windows Authentication or Use Server Authentication (depending on how the instance of SQL Server is configured).
In the Database drop-down list, select <new>. Give your database a name, accept the defaults, click OK, and then click Next.
On the next page, leave the radio button Copy table(s) and view(s) from the source database selected and click Next.
On the next screen, select the check box next to the table that contains your location data. Leave the default value in the Destination column (it will look something like [dbname].[dbo].[tablename]), and then click Next.
On the next screen accept the defaults, click Next, and then click Finish to exit the wizard.
You should see the status of the import operation as your data is imported into SQL Server.
After the import operation is finished, your new database will be listed in the Enterprise Manager Databases node (you may have to refresh the list first).
The next step is to create the stored procedure, which is called sp_FindNearby. The stored procedure applies the remainder of the law of cosines algorithm. It takes the address the user provides, calculates the distance between the address and each business location within the specified search radius, and then orders the results by distance.
When you run sp_FindNearby, you must supply four input parameters:
To create the stored procedure
In the left pane of Enterprise Manager, find your database, and then expand it.
Right-click Stored Procedures, and then click New Stored Procedure.
In the text box that opens, type following procedure. In the procedure, [TABLENAME] refers to the name of your database table. Ensure sure that the list of fields in the SELECT statement match the fields in your table.
CREATE PROCEDURE dbo.sp_FindNearby
@CenterLat float,
@CenterLon float,
@SearchDistance float,
@EarthRadius float
AS
declare @CntXAxis float
declare @CntYAxis float
declare @CntZAxis float
set @CntXAxis = cos(radians(@CenterLat)) * cos(radians(@CenterLon))
set @CntYAxis = cos(radians(@CenterLat)) * sin(radians(@CenterLon))
set @CntZAxis = sin(radians(@CenterLat))
select *, ProxDistance = @EarthRadius * acos( XAxis*@CntXAxis +
YAxis*@CntYAxis + ZAxis*@CntZAxis)
from [TABLENAME]
where @EarthRadius * acos( XAxis*@CntXAxis + YAxis*@CntYAxis +
ZAxis*@CntZAxis) <= @SearchDistance
order by ProxDistance ASC
GO
Click OK.
You should now see the sp_FindNearby stored procedure listed in the right pane.
Creating a user specifically for the purposes of proximity searches is not required, but it is recommended. Because a stored procedure is a snippet of code that executes on your server, limiting access to it and giving users only the permissions they need is a good idea. This step creates a new user, gives the user read-only permissions on the data, and permission to execute the sp_FindNearby stored procedure.
To create a new login
Storing and managing your data locally can be beneficial if you have sensitive or frequently-changing data or very large datasets. By implementing proximity searches through SQL Server, you can maintain your data in a secure, local environment while taking advantage of the wide variety of location-based services that MapPoint Web Service offers.
Justin Southall is a Web Development Engineer in the Microsoft MapPoint Business Unit, and is a member of the Professional Services team.
Please sign in to use this experience.
Sign in