真失望

微軟的 Virtual Earth 沒什麼中文化,MapPoint 似乎也找不到中文的地址…

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim findService As New net.mappoint.staging.FindServiceSoap()
    findService.Credentials = New System.Net.NetworkCredential("你申請的 MapPoint 帳號", "密碼")  ‘MapPoint 用 Email 寄後,再要你上網修改密碼,但居然沒有忘記帳號密碼的功能(我找不到)
    Dim add As New Address
    add.AddressLine = "" ‘"2855 Telegraph Avenue" ‘"No.3, Sec. 2, Ren-ai Rd., Luzhu Township" ‘"仁愛路2段3號"
    add.PrimaryCity = "Taipei" ‘"Berkeley" ‘"Taoyuan County" ‘"桃園縣"
    add.Subdivision = "TW" ‘"CA" ‘"TW" ‘"蘆竹鄉"
    add.PostalCode = "110" ‘"94705" ‘"338"
    add.CountryRegion = "Taiwan" ‘"USA" ‘"Taiwan"

    Dim findSpec As New FindAddressSpecification
    findSpec.InputAddress = add
    findSpec.DataSourceName = "MapPoint.AP" ‘ "MapPoint.NA" ‘"MapPoint.AP"
    Dim findOp As New FindOptions
    findOp.ResultMask = FindResultMask.LatLongFlag

    Dim findRg As New FindRange
    findRg.StartIndex = 0
    findRg.Count = 1
    findOp.Range = findRg

    findSpec.Options = findOp

    Dim findRsts As FindResults = findService.FindAddress(findSpec)
    Dim strWKT As String
    If findRsts.Results.Length > 0 Then
        strWKT = "POINT(" & findRsts.Results(0).FoundLocation.LatLong.Longitude & " " & _
        findRsts.Results(0).FoundLocation.LatLong.Latitude & ")"
    Else
        strWKT = "沒有符合的紀錄"
    End If
    MessageBox.Show(strWKT)
End Sub

 

台灣的地址不管是輸中文或英文,乃至於只找城市,都找不到,若輸入美國的地址就可以…

看來,微軟全球的地理服務還差 Google 一截,但 Google 似乎沒有提供 Web Service,做上述的查詢,需要透過網頁Get/JScript/Ajax方式完成上述查詢,這讓 Coding 繞一層,也很不方便。

不知大家是否有好的解法…

目前,我仍存取 Google

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Types;
using System.Net;
using System.IO;
using System.Text;

public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlGeography GetGeographyPoint(string strAddress)
    {

//透過 Google API 查詢經緯度,並將包含經緯度資料的點以 SqlGeography 物件執行個體傳回,
//原始碼參考 : http://www.dotblogs.com.tw/chhuang/archive/2008/04/03/2593.aspx
string strGoogle = string.Format(
              "http://maps.google.com/maps/geo?q={0}&output={1}&key={2}",
              strAddress, "csv",
              "要傳入你自行到 Google 註冊取回的 Key");

        WebClient wc = new WebClient();
        Stream s = wc.OpenRead(strGoogle);
        StreamReader sr = new StreamReader(s, Encoding.UTF8);
        string strRet = sr.ReadToEnd();
        //解析 200,8,25.033408,121.564099  (HTTP status code, accuracy, latitude, longitude)
        //經由 SqlGeography.Parse 函數轉成 Geography 物件 POINT(longitude latidue)
        SqlGeography geo = SqlGeography.Parse(
            string.Format("POINT({0} {1})",
            strRet.Split(‘,’)[3],
            strRet.Split(‘,’)[2]));
        return geo;
    }
};

將上述的 SQLCLR 函數註冊到 SQL Server

EXEC sp_configure ‘clr enabled’,1
reconfigure
go

ALTER DATABASE Spatial SET TRUSTWORTHY ON

CREATE ASSEMBLY GeoCoding FROM ‘C:Beginning Spatial with SQL Server 2008myGeoCodingGeoCodingbinDebugGeoCoding.dll’
WITH PERMISSION_SET=EXTERNAL_ACCESS
GO

CREATE FUNCTION dbo.GetGeographyPoint(@Address NVARCHAR(1000))
RETURNS GEOGRAPHY
AS EXTERNAL NAME GeoCoding.UserDefinedFunctions.GetGeographyPoint

DECLARE @geo GEOGRAPHY
SET @geo=dbo.GetGeographyPoint(N’桃園縣蘆竹鄉仁愛路2段3號’)
SELECT @geo ‘二進位’ ,@geo.STAsText() ‘(經度 緯度)’

執行結果如下

image

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: