一、加入GeocodeService的Web服务引用
地理编码服务(GeocodeService)是以WCF技术公布的一个Web服务,地图编码服务提供了以一个有效的物理地址在地图上匹配其相应的地图地址(既地理经度和纬度坐标)和以地理经度和纬度坐标进行反向匹配物理地址路径的功能。要使用该服务需加入该服务(http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc)的Web服务引用,例如以下图
二、通过地址获得经纬度的code:
GeocodeServiceClient geocodeService = new GeocodeServiceClient();
GeocodeRequest geocodeRequest = new GeocodeRequest();
geocodeRequest.Credentials = new Credentials();
geocodeRequest.Credentials.ApplicationId = "Arn2694QD8zXQJGN_IgecLbotcSVT1gTyRFfNSdPsIOO - yWjkkZRbwKcNEpCfelq";
geocodeRequest.Query = sAddress; ConfidenceFilter[] filters = new ConfidenceFilter[1];
filters[0] = new ConfidenceFilter();
filters[0].MinimumConfidence = Confidence.Low; GeocodeOptions geocodeOptions = new GeocodeOptions();
geocodeOptions.Filters = filters;
geocodeRequest.Options = geocodeOptions; GeocodeResponse geocodeResponse = new GeocodeResponse();
geocodeResponse = geocodeService.Geocode(geocodeRequest);
if (geocodeResponse.Results != null)
{
int iLength = geocodeResponse.Results.Length;
if (iLength >= 1)
{
string sConfidence = geocodeResponse.Results[0].Confidence.ToString();
if (sConfidence == "High")
{
string sState = geocodeResponse.Results[0].Address.AdminDistrict;
string sCity = geocodeResponse.Results[0].Address.Locality;
string sZip = geocodeResponse.Results[0].Address.PostalCode;
string sLat = geocodeResponse.Results[0].Locations[0].Latitude.ToString();
string sLon = geocodeResponse.Results[0].Locations[0].Longitude.ToString();
string sqlExist = "select * from mapping_geodata_boundary where code='NJ0415' and boundary.STContains(geometry::STGeomFromText('POINT(" + sLon + " " + sLat + ")', 0))=1";
DataTable dtExist = _dataAccess.GetTables(sqlExist);
if (dtExist.Rows.Count > 0)
{
//Update
string sqlUpdate = "update mapping_parcels set city_state_zip=owner_citystate where city is null and fid=" + id;
_dataAccess.ExcuateSQL(sqlUpdate);
}
}
}
}
}
三、通过经纬度获得地址的Code:
ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();
reverseGeocodeRequest.Credentials = new Credentials();
reverseGeocodeRequest.Credentials.ApplicationId = "Arn2694QD8zXQJGN_IgecLbotcSVT1gTyRFfNSdPsIOO - yWjkkZRbwKcNEpCfelq"; Location point = new Location();
point.Latitude = double.Parse(lat);
point.Longitude = double.Parse(lon);
reverseGeocodeRequest.Location = point; GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest); if (geocodeResponse.Results != null)
{
int iLength = geocodeResponse.Results.Length;
if (iLength >= 1)
{
string sConfidence = geocodeResponse.Results[0].Confidence.ToString();
if (sConfidence == "Medium" || sConfidence == "High")
{
string sAddress = geocodeResponse.Results[0].DisplayName;
if (sAddress.Contains("'"))
{
sAddress = sAddress.Replace("'", "''");
}
string sStreetName = geocodeResponse.Results[0].Address.AddressLine;
//string sState = geocodeResponse.Results[0].Address.AdminDistrict;
string sCity = geocodeResponse.Results[0].Address.Locality;
string sZip = geocodeResponse.Results[0].Address.PostalCode;
string sLat = geocodeResponse.Results[0].Locations[0].Latitude.ToString();
string sLon = geocodeResponse.Results[0].Locations[0].Longitude.ToString();
string sMatchCodes = geocodeResponse.Results[0].MatchCodes[geocodeResponse.Results[0].MatchCodes.Length - 1].ToString();
string sqlExist = "select * from mapping_geodata_boundary where code='NJ0415' and boundary.STContains(geometry::STGeomFromText('POINT(" + sLon + " " + sLat + ")', 0))=1";
DataTable dtExist = _dataAccess.GetTables(sqlExist);
if (dtExist.Rows.Count > 0)
{
//Update
string sqlUpdate = "update mapping_parcels set shape_street_name='" + sStreetName
+ "',shape_city='" + sCity + "',shape_address='" + sAddress
+ "',shape_zip='" + sZip + "',shape_matchcode='" + sMatchCodes + "' where fid=" + id;
_dataAccess.ExcuateSQL(sqlUpdate);
}
}
}
}