Xamarin Essentials VS Google 地图 API
Xamarin Essentials VS Google Maps API
尝试根据用户输入的邮政编码查找城市和州时,使用这两种方法有什么区别。据我所知,在使用 Essentials 时,您必须拨打两次电话,一次是获取邮政编码 longitude/latitude,另一次是从 longitude/latitude 获取更多信息。
public async Task GetLocationEssentials(string zip)
{
var locations = await Geocoding.GetLocationsAsync(zip);
var location = locations?.FirstOrDefault();
if (location != null)
{
Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
}
try
{
var lat = location.Latitude;
var lon = location.Longitude;
var placemarks = await Geocoding.GetPlacemarksAsync(lat, lon);
var placemark = placemarks?.FirstOrDefault();
if (placemark != null)
{
var geocodeAddress =
$"AdminArea: {placemark.AdminArea}\n" +
$"CountryCode: {placemark.CountryCode}\n" +
$"CountryName: {placemark.CountryName}\n" +
$"FeatureName: {placemark.FeatureName}\n" +
$"Locality: {placemark.Locality}\n" +
$"PostalCode: {placemark.PostalCode}\n" +
$"SubAdminArea: {placemark.SubAdminArea}\n" +
$"SubLocality: {placemark.SubLocality}\n" +
$"SubThoroughfare: {placemark.SubThoroughfare}\n" +
$"Thoroughfare: {placemark.Thoroughfare}\n";
Console.WriteLine(geocodeAddress);
}
}
catch (FeatureNotSupportedException fnsEx)
{
// Feature not supported on device
}
catch (Exception ex)
{
// Handle exception that may have occurred in geocoding
}
}
第二个选项是获取 google 映射 API 密钥并使用邮政编码调用他们的 api 但这需要获取 api 密钥和那么您每天只能拨打 2500 次电话。
public async System.Threading.Tasks.Task GetLocationAsync(string zip)
{
string url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + zip + "&sensor=true";
var client = new HttpClient();
// 'using' forces proper cleanup after finishing the operation
using (var response = await client.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
var jsonResult = await response.Content.ReadAsStringAsync();
//var jsonResponse = JsonConvert.DeserializeObject<>(jsonResult);
}
}
}
使用 googleapi 调用除了只需调用 1 次之外还有其他好处吗?如果您拨打很多电话,必需品会把您拒之门外吗?
核心区别在于 Essentials 使用每个平台的 "native" 功能进行地理定位服务,因此对于相同的 address/LatLong 位置,您的结果会因平台而异。
在 iOS 上,使用了 CLGeocoder
,因此使用了 Apple 的地理位置数据库,就像明智的 Android 使用 Geocoder
(结果更简单和稀疏比较 Googles API) 和 UWP 使用 BasicGeoposition
(需要地图标记)。
Google 直接地理位置 API 提供跨平台的一致请求集和(注意:基于意见的)更全面的结果集。
尝试根据用户输入的邮政编码查找城市和州时,使用这两种方法有什么区别。据我所知,在使用 Essentials 时,您必须拨打两次电话,一次是获取邮政编码 longitude/latitude,另一次是从 longitude/latitude 获取更多信息。
public async Task GetLocationEssentials(string zip)
{
var locations = await Geocoding.GetLocationsAsync(zip);
var location = locations?.FirstOrDefault();
if (location != null)
{
Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
}
try
{
var lat = location.Latitude;
var lon = location.Longitude;
var placemarks = await Geocoding.GetPlacemarksAsync(lat, lon);
var placemark = placemarks?.FirstOrDefault();
if (placemark != null)
{
var geocodeAddress =
$"AdminArea: {placemark.AdminArea}\n" +
$"CountryCode: {placemark.CountryCode}\n" +
$"CountryName: {placemark.CountryName}\n" +
$"FeatureName: {placemark.FeatureName}\n" +
$"Locality: {placemark.Locality}\n" +
$"PostalCode: {placemark.PostalCode}\n" +
$"SubAdminArea: {placemark.SubAdminArea}\n" +
$"SubLocality: {placemark.SubLocality}\n" +
$"SubThoroughfare: {placemark.SubThoroughfare}\n" +
$"Thoroughfare: {placemark.Thoroughfare}\n";
Console.WriteLine(geocodeAddress);
}
}
catch (FeatureNotSupportedException fnsEx)
{
// Feature not supported on device
}
catch (Exception ex)
{
// Handle exception that may have occurred in geocoding
}
}
第二个选项是获取 google 映射 API 密钥并使用邮政编码调用他们的 api 但这需要获取 api 密钥和那么您每天只能拨打 2500 次电话。
public async System.Threading.Tasks.Task GetLocationAsync(string zip)
{
string url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + zip + "&sensor=true";
var client = new HttpClient();
// 'using' forces proper cleanup after finishing the operation
using (var response = await client.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
var jsonResult = await response.Content.ReadAsStringAsync();
//var jsonResponse = JsonConvert.DeserializeObject<>(jsonResult);
}
}
}
使用 googleapi 调用除了只需调用 1 次之外还有其他好处吗?如果您拨打很多电话,必需品会把您拒之门外吗?
核心区别在于 Essentials 使用每个平台的 "native" 功能进行地理定位服务,因此对于相同的 address/LatLong 位置,您的结果会因平台而异。
在 iOS 上,使用了 CLGeocoder
,因此使用了 Apple 的地理位置数据库,就像明智的 Android 使用 Geocoder
(结果更简单和稀疏比较 Googles API) 和 UWP 使用 BasicGeoposition
(需要地图标记)。
Google 直接地理位置 API 提供跨平台的一致请求集和(注意:基于意见的)更全面的结果集。