Hafta sonu bir iphone projesi için yakınımdaki kayıtlı işyerlerini sorgulayabilmem gerekiyordu. Ben de oturup MS SQL Server için sadece T-SQL kullanarak bir user-defined function yazdım :) Tüm bu işlemleri veritabanının içinden yapabilmek çok eğlenceli :) Tam bir ego tatmini oldu :)

CREATE FUNCTION dbo.GetNearStores (
@Latitude FLOAT,
@Longitude FLOAT,
@MaximumDistance FLOAT
)
RETURNS
@ResultTable table (
[StoreLocationId] [uniqueidentifier],
[StoreLocationName] [nvarchar] (50),
[StoreLocationLatitude] [FLOAT],
[StoreLocationLongitude] [FLOAT],
[StoreId] [uniqueidentifier],
[StoreName] [nvarchar] (1024),
[StoreShortDescription] [nvarchar] (1024),
[StoreDescription] [nvarchar] (1024),
[Distance] [FLOAT]
)
AS
BEGIN
DECLARE @StoreLocationId UNIQUEIDENTIFIER
DECLARE @StoreLocationName NVARCHAR(50)
DECLARE @StoreLocationLatitude FLOAT
DECLARE @StoreLocationLongitude FLOAT
DECLARE @StoreId UNIQUEIDENTIFIER
DECLARE @StoreName NVARCHAR(1024)
DECLARE @StoreShortDescription NVARCHAR(1024)
DECLARE @StoreDescription NVARCHAR(1024)
DECLARE cur CURSOR
FOR
SELECT
StoreLocationId,
StoreLocationName,
StoreLocationLatitude,
StoreLocationLogitude,
StoreId,
StoreName,
StoreShortDescription,
StoreDescription
FROM VWStoreLocations
OPEN cur
FETCH NEXT FROM cur
INTO @StoreLocationId,
@StoreLocationName,
@StoreLocationLatitude,
@StoreLocationLongitude,
@StoreId,
@StoreName,
@StoreShortDescription,
@StoreDescription
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @latitudeA FLOAT
DECLARE @longitudeA FLOAT
DECLARE @latitudeB FLOAT
DECLARE @longitudeB FLOAT
SELECT @latitudeA = @latitude
SELECT @longitudeA = @longitude
SELECT @latitudeB = @StoreLocationLatitude;
SELECT @longitudeB = @StoreLocationLongitude;
DECLARE @pi80 FLOAT
SELECT @pi80 = PI() / 180
DECLARE @lat1 FLOAT
SELECT @lat1 = @latitudeA * @pi80
DECLARE @lng1 FLOAT
SELECT @lng1 = @longitudeA * @pi80
DECLARE @lat2 FLOAT
SELECT @lat2 = @latitudeB * @pi80
DECLARE @lng2 FLOAT
SELECT @lng2 = @longitudeB * @pi80
DECLARE @r FLOAT
SELECT @r = 6372.797
DECLARE @dlat FLOAT
SELECT @dlat = @lat2 - @lat1
DECLARE @dlng FLOAT
SELECT @dlng = @lng2 - @lng1
DECLARE @a FLOAT
SELECT @a = SIN(@dlat / 2) * SIN(@dlat / 2) + COS(@lat1) * COS(@lat2) * SIN(@dlng / 2) * SIN(@dlng / 2)
DECLARE @c FLOAT
SELECT @c = 2 * ATN2(SQRT(@a), SQRT(1 - @a))
DECLARE @distance FLOAT
SELECT @distance = @r * @c
IF (@distance < @MaximumDistance)
BEGIN
INSERT INTO @ResultTable
(StoreLocationId,
StoreLocationName,
StoreLocationLatitude,
StoreLocationLongitude,
StoreId,
StoreName,
StoreShortDescription,
StoreDescription,
Distance)
VALUES
(@StoreLocationId,
@StoreLocationName,
@StoreLocationLatitude,
@StoreLocationLongitude,
@StoreId,
@StoreName,
@StoreShortDescription,
@StoreDescription,
@distance)
END
FETCH NEXT FROM cur
INTO @StoreLocationId,
@StoreLocationName,
@StoreLocationLatitude,
@StoreLocationLongitude,
@StoreId,
@StoreName,
@StoreShortDescription,
@StoreDescription
END
CLOSE cur
DEALLOCATE cur
RETURN;
END
Sunucudan sql cümlesini çekmek için de asenkron güvenli bir yöntem kullanıyorum, 10 saniye timeout süreli bir asenkron bağlantı açıp sorgulamayı GET parametreleriyle yapıyorum ama POST işlemi de veri güvenliği ve diğer bazı sebepler için tabiki de kullanılabilir.
NSString *myRequestString = @"";
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
NSString *url_ = [NSString stringWithFormat:@"%@?op=getstorelocations&dist=%d&lat=%.6f&lon=%.6f", url, distance, lat, lon];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url_]];
[request setHTTPMethod: @"GET"];
[request setHTTPBody: myRequestData];
[request setTimeoutInterval:10.0];
if ([[NSURLConnection alloc] initWithRequest:request delegate:self])
{
receivedData = [[NSMutableData data] retain];
}
Sevgiler.