0
我想通过存储过程调用Google的距离矩阵API ....以便我将两个参数作为起源和目的地,并从SP取得结果和时间。 ...在此先感谢通过存储过程调用Google矩阵距离api通过存储过程sql服务器
我想通过存储过程调用Google的距离矩阵API ....以便我将两个参数作为起源和目的地,并从SP取得结果和时间。 ...在此先感谢通过存储过程调用Google矩阵距离api通过存储过程sql服务器
I Have Done It...I Am Posting It If Later Any Body Need This....I Make An SP Here...Pass Pin Codes as Parameters to it and get Distance As Result..
USE [CalculateDistance]
GO
/****** Object: StoredProcedure [dbo].[CalculateDistence] Script Date: 12/1/2014 12:49:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Rajay Sachdeva>
-- Create date: <Create 25/11/2014,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[CalculateDistence] --'','','','37064','','','','78701',''
(
@ToAddress varchar(100) =null,
@ToCity varchar(100)=null,
@ToState varchar(100)=null,
@ToPostCode varchar(100)=null,
@FromAddress varchar(100)=null,
@FromCity varchar(100)=null,
@FromState varchar(100)=null,
@FromPostCode varchar(100)=null,
@DistanceInKilometers varchar(100) output
)
AS
Declare @Object as Int;
Declare @ResponseText as Varchar(8000);
Declare @serviceUrl as varchar(500)
set @serviceUrl = 'http://maps.googleapis.com/maps/api/distancematrix/xml?origins=' [email protected][email protected][email protected][email protected]+
'&destinations=' [email protected][email protected][email protected]+ @FromPostCode +'&mode=driving&language=en-EN&units=metric;'
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',
@serviceUrl, --Your Web Service Url (invoked)
'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
Declare @Response as XML
--Select @ResponseText as XMLList
SET @Response = CAST(@ResponseText AS XML);
Declare @Status as varchar(20)
Declare @Distance as varchar(20)
set @Status= @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'varchar(20)')
print @Status
if(@Status='ZERO_RESULTS')
Begin
set @[email protected]
End
else
Begin
set @[email protected]('(DistanceMatrixResponse/row/element/distance/text)[1]', 'varchar(20)')
End
--Select @Response.value('(DistanceMatrixResponse/row/element/distance/text)[1]', 'varchar(10)') as Distance
select @Distance as Distance
也许有点晚了,但是这也适用:
CREATE FUNCTION [dbo].[CalculateGoogleDistance]
(
@Latitude1 nvarchar(1000),
@Longitude1 nvarchar(1000),
@Latitude2 nvarchar(1000),
@Longitude2 nvarchar(1000)
)
returns nvarchar(1000)
as
begin
DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POINT(' + @Latitude1 + ' ' + @Longitude1 + ')', 4326);
SET @h = geography::STGeomFromText('POINT(' + @Latitude2 + ' ' + @Longitude2 + ')', 4326);
DECLARE @dist float
DECLARE @distType varchar(10)
if FLOOR(CONVERT(float, @g.STDistance(@h))) < 100
BEGIN
SET @dist = FLOOR(CONVERT(float, @g.STDistance(@h)))
SET @distType = 'cm'
END
ELSE if FLOOR(CONVERT(float, @g.STDistance(@h))) < 1000
BEGIN
SET @dist = FLOOR(CONVERT(float, @g.STDistance(@h)))
SET @distType = 'm'
END
ELSE
BEGIN
SET @dist = FLOOR(CONVERT(float, @g.STDistance(@h))/1000)
SET @distType = 'km'
END
return CAST(@dist as nvarchar(1000)) + ' ' + @distType
end
使用直接距离 – ontranet 2015-03-02 14:15:52
谢谢你的答案... – 2015-03-04 09:38:10