First time try google map related, the requirement asks to provide a step by step route for sales, so that they are easy to work with them.
So first design the API.
[OperationContract]
GetDirectionResponse GetDirection(GetDirectionRequest request);
[DataContract]
public class GetDirectionRequest : BaseServiceRequest
{
[DataMember]
public ServiceAddress Origin { get; set; }
[DataMember]
public ServiceAddress Destination { get; set; }
[DataMember]
public bool? Sensor { get; set; }
[DataMember]
public GMapTravelMode? Mode { get; set; }
public static implicit operator GetDirectionReqt(GetDirectionRequest request)
{
return new GetDirectionReqt
{
Origin = request.Origin,
Destination = request.Destination,
Sensor = request.Sensor ?? false,
Mode = request.Mode ?? GMapTravelMode.Driving
};
}
}
[DataContract]
public class GetDirectionResponse : BaseServiceResponse
{
[DataMember(Name = "routes")]
public List<GMapRoute> Routes { get; set; }
[DataMember(Name = "status")]
public string Status { get; set; }
public static implicit operator GetDirectionResponse(GetDirectionResp response)
{
return new GetDirectionResponse
{
Status = response.Status,
Routes = response.Routes,
IsSuccess = response.IsSuccess,
ErrorMessage = response.ErrorMessage
};
}
}
Then the implementation (there is another layer between service and helper class, skip here...)
public GetDirectionResp GetDirection(GetDirectionReqt request)
{
return GMapHelper.QueryRoute(request.Origin, request.Destination, request.Mode, request.Sensor);
}
And the GMapHelper code
public static GetDirectionResp QueryRoute(ServiceAddress origin, ServiceAddress destination, GMapTravelMode travelMode = GMapTravelMode.Driving, bool sensor = false)
{
var gmapRouteQueryUrl = ConfigurationManager.AppSettings["GMap_Route_Query_URL"];
var requestUri = new Uri(string.Format(gmapRouteQueryUrl, origin, destination, travelMode, sensor.ToString().ToLower()));
var response = new GetDirectionResp();
try
{
var responseData = new WebClient().DownloadData(requestUri);
var serializer = new DataContractJsonSerializer(typeof(GetDirectionResp));
var stream = new MemoryStream(responseData, false);
response = (GetDirectionResp)serializer.ReadObject(stream);
if (response.Status == "OK")
{
response.IsSuccess = true;
}
else
{
response.IsSuccess = false;
response.ErrorMessage = string.Format("Unable to get route(s) from {0} to {1}.", origin, destination);
}
}
catch (Exception ex)
{
string errMessage = string.Format("Unexpected exception getting route(s) from {0} to {1}", origin, destination);
Logger.Error(string.Format("{0}: {1}", MethodBase.GetCurrentMethod().Name, errMessage), ex);
response.IsSuccess = false;
response.ErrorMessage = string.Format("{0}, Message: {1}", errMessage, ex.Message);
}
return response;
}
oh, almost forget the google map directions request url:
<!-- Google Map -->
<add key="GMap_Route_Query_URL" value="http://maps.google.com/maps/api/directions/json?origin={0}&destination={1}&mode={2}&sensor={3}" />
And attach some official material web sites.
https://developers.google.com/maps/documentation/webservices/index
https://developers.google.com/maps/documentation/directions/
The other thing about usage limits
引用
Use of the Google Directions API is subject to a query limit of 2,500 directions requests per day. Each directions search will count as a single request against your daily quota when the mode of transportation is driving, walking or cycling. Searching for transit directions will count as 4 requests.
分享到:
相关推荐
谷歌地图方向 使用 Google Maps API 查找路线
这是一个用Kotlin写的小型图书馆,可以利用Google Directions API调用并将响应转换为PolylineOptions,并准备添加到GoogleMap中。 我为什么要使用它? 好吧,您不必这样做。 但是,如果您已经阅读过此书,则-如果...
var map = require ( 'google_directions' ) ; var params = { // REQUIRED origin : "" , destination : "" , key : "" , // OPTIONAL mode : "" , avoid : "" , language : "" , units : "" , region :...
此插件充当Google Map的Directions Services API的帮助程序代码,因此您不必费力地理解API代码。 步骤1:在代码中包含所需的JS文件。 如果已经包含jQuery,则不要包含jQuery。 另外请注意,还包括Google Maps API。...
This project allows you to calculate the direction between two locations and display the route on a Google Map using the Google Directions API - This project isn't actively been maintained. Sample ...
Google-Directions-Android此项目可让您计算两个位置之间的方向,并使用Google Directions API在Google Map上显示路线-该项目并未得到积极维护。 Google-Directions-Android此项目可让您计算两个位置之间的方向,并...
Other web services: AMap Search, Bing Maps Elevation API, Bing Maps Location API, HERE Routing API, Open Route Service Directions, Open Route Service Geocoding, Open Street Map Nominatim, Open Street...
Other web services: AMap Search, Bing Maps Elevation API, Bing Maps Location API, HERE Routing API, Open Route Service Directions, Open Route Service Geocoding, Open Street Map Nominatim, Open Street...
•其他Web服务:AMap Search,Bing Maps Elevation API,Bing Maps Location API,HERE Routing API,Open Route Service Directions,Open Route Service Geocoding,Open Street Map Nominatim,Open Street Map ...
谷歌地图– google-maps Citymapper – citymapper 尤伯杯- uber Lyft – lyft 公交应用程序– transit 卡车地图– truckmap Waze的- waze Yandex.Navi – yandex Moovit – moovit Yandex出租车– yandex...
The most recent Google Maps API for Adobe Flash is the map_1_9a.swc and for Adobe Flex or Adobe Air it is the map_flex_1_9a.swc one. It fixes two bugs: Geocoding and Directions fail if swf is hosted ...
如果可能,显示新地址和第二个地址(工作、学校等)之间的交通选项,并将 Google Directions API 集成为另一个地图层。 请参阅 Google Routes 示例代码 ( ) 和现场演示 ( ) 另请参阅非功能模型以了解第二个地址的所...
Map.java: Helper class for getting directions in Google Maps App from current to point to defined point via another app. POST.java: Helper class for sending POST requests. GET.hava: Helper class for ...
农业方向 是的指令(非官方) 角度的Google Map API 如何使用? :backhand_index_pointing_right:信用安装使用npm install命令完成npm install : 使用npm npm install --save @agm/core agm-direction 用纱yarn add...
google_maps:for用于Rust编程语言的非官方Google Maps Platform客户端库
要查看Sygic Map矢量图块的简单集成演示,请访问 (, ) 要查看Sygic地图上Google编码折线的简单集成演示,请使用 (此处提供代码, jsfiddle可在此处使用) 要查看Sygic地图上多个Google编码折线的集成演示,请...
越野导航系统在线可见度图算法无需图形预计算的A *寻路...就是这样的Google路由引擎(及所有其他路由)的工作方式。 我准备改变这一点。 适用范围: 扩展其他路由引擎的功能筑路救援和军事行动计划远足和旅游路线规划