C# 无法隐式转换类型';Plantool.xRoute.LineString[]和#x27;至';Plantool.xMap.LineString[]和#x27;
情况: 我有3个网络参考(xMap、xLocation、xRoute) xMap专用于生成地图。 xLocation专用于定位位置。 xRoute专用于生成路由 我使用一个简单的GUI来显示地图,并输入起点和终点路线的位置 这些是我的错误 错误1无法隐式转换类型“Plantool.xRoute.LineString[]” 至“Plantool.xMap.LineString[]” 错误2与的最佳重载方法匹配 'Plantool.xMap.XMapWSService.renderMapBoundingBox(Plantool.xMap.BoundingBox, Plantool.xMap.MapParams、Plantool.xMap.ImageInfo、, Plantool.xMap.Layer[],bool,Plantool.xMap.CallerContext)“有一些 无效参数 错误3参数“1”:无法从转换 “Plantool.xRoute.BoundingBox”到“Plantool.xMap.BoundingBox” 我猜ptvxserver的复制方法/特性/etc与xMap相同,xLocate和xRoute是可选模块。这可能是一个简单的答案,有解决办法吗 我正在寻找一个头部的长途回家,并花费额外的上瘾半个小时的加班费在这个代码。嗨,我是新来的 在我班上,他比我低C# 无法隐式转换类型';Plantool.xRoute.LineString[]和#x27;至';Plantool.xMap.LineString[]和#x27;,c#,xserver,C#,Xserver,情况: 我有3个网络参考(xMap、xLocation、xRoute) xMap专用于生成地图。 xLocation专用于定位位置。 xRoute专用于生成路由 我使用一个简单的GUI来显示地图,并输入起点和终点路线的位置 这些是我的错误 错误1无法隐式转换类型“Plantool.xRoute.LineString[]” 至“Plantool.xMap.LineString[]” 错误2与的最佳重载方法匹配 'Plantool.xMap.XMapWSService.renderMapBoundi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Plantool.xMap;
using Plantool.xLocate;
using Plantool.xRoute;
namespace Plantool
{
class ClassMap
{
/*
* xServer clients
*/
private static XMapWSService xMapClient = new XMapWSService();
private static XRouteWSService xRouteClient = new XRouteWSService();
private static XLocateWSService xLocateClient = new XLocateWSService();
/* getLocation()
* Input: Address string
* Output: WKT (Well-Known-Text) Location
* Edited 20/12/12 - Davide Nguyen
*/
public string getLocation(string input)
{
// create the adress object
string[] address = input.Split(',');
Address addr = new Address();
addr.country = address[0];
addr.city = address[1];
addr.postCode = address[2];
// call findAddress on the xLocate server
// only the first argument of findAddress is mandatory,
// all others are nullable (see xLocate API documentation)
AddressResponse response = xLocateClient.findAddress(addr, null, null, null, null);
string result = "";
foreach (ResultAddress ra in response.wrappedResultList)
{
result += String.Format("POINT( {0} {1}) ", ra.coordinates.point.x, ra.coordinates.point.y);
}
string result2 = result.Replace(",", ".");
return result2;
}
/* route()
* Input: Start address, Destination address
* Output: string[] [0] DISTANCE [1] TIME [2] MAP
* Edited 20/12/12 - Davide Nguyen
*/
public string[] route(string startlocation, string destination)
{
#region WaypointDesc[]
// create the WayPoint for the Start
// ATTENTION: Here at the object WaypointDesc the parameters are not named
// "coords" as described within the documentation but
// "wrappedCoords"
WaypointDesc wpdStart = new WaypointDesc();
// please note that this has to be an array of Point...
wpdStart.wrappedCoords = new xRoute.Point[] { new xRoute.Point() };
wpdStart.wrappedCoords[0].wkt = getLocation(startlocation);
// Waypoint for the Destination...
WaypointDesc wpdDestination = new WaypointDesc();
wpdDestination.wrappedCoords = new xRoute.Point[] { new xRoute.Point() };
wpdDestination.wrappedCoords[0].wkt = getLocation(destination);
// create a new array of WayPointDescriptions and fill it with Start and Destination
WaypointDesc[] waypointDesc = new WaypointDesc[] { wpdStart, wpdDestination };
#endregion
try
{
// Route
Route route = calculateRoute(waypointDesc);
// Map
string DisplayMapURL = createMap(waypointDesc, route);
// get route info
string[] routeinfo = getRouteInfo(waypointDesc);
// Create the result
string[] result = new string[3];
result[0] = routeinfo[0]; // Distance
result[1] = routeinfo[1]; // Time
result[2] = DisplayMapURL;// Map URL
// Return the result
return result;
}
catch
{
throw new NotImplementedException();
}
}
/* getRouteInfo()
* Input: WaypointDesc[]
* Output: string mapURL
* Edited 20/12/12 - Davide Nguyen
*/
private static string createMap(WaypointDesc[] waypointDesc, Route route)
{
#region boundingBox
// Set boundingBox fand use corners from the calculated route
xRoute.BoundingBox boundingBox = new xRoute.BoundingBox();
boundingBox.leftTop = route.totalRectangle.rightTop;
boundingBox.rightBottom = route.totalRectangle.leftBottom;
#endregion
#region mapParams
// Build mapParams
MapParams mapParams = new MapParams();
mapParams.showScale = true;
mapParams.useMiles = false;
#endregion
#region imageInfo
// Create imageInfo and set the frame size and image format. NOTE: 1052; 863
ImageInfo imageInfo = new ImageInfo();
imageInfo.format = ImageFileFormat.PNG;
imageInfo.height = 1052;
imageInfo.width = 863;
imageInfo.imageParameter = "";
#endregion
#region layers
// Create a line from the calculated route
xRoute.LineString[] lineStrings = new xRoute.LineString[] { route.polygon };
Lines[] lines = new Lines[1];
LineOptions options = new LineOptions();
LinePartOptions partoptions = new LinePartOptions();
partoptions.color = new Color();
partoptions.visible = true;
partoptions.width = -10;
options.mainLine = partoptions;
lines[0] = new Lines();
lines[0].wrappedLines = lineStrings; //NEED HELP
lines[0].options = options;
// Define customLayer that contains the object lines and set layers.
CustomLayer customLayer = new CustomLayer();
customLayer.visible = true;
customLayer.drawPriority = 100;
customLayer.wrappedLines = lines;
customLayer.objectInfos = ObjectInfoType.NONE;
customLayer.centerObjects = true;
Layer[] layers = new Layer[] { customLayer };
#endregion
#region includeImageInResponse
// Set argument includeImageInResponse to false (default).
Boolean includeImageInResponse = false;
#endregion
// Return object map using the following method.
Map map = xMapClient.renderMapBoundingBox(boundingBox, mapParams, imageInfo, layers, includeImageInResponse, null); // NEED HELP
// Retrieve the image
string result = "http://" + map.image.url;
// Return the drawn map
return result;
}
/* getRouteInfo()
* Input: WaypointDesc[]
* Output: string[] [0] Distance in M [1] Time in H:M:S:MS
* Edited 20/12/12 - Davide Nguyen
*/
private string[] getRouteInfo(WaypointDesc[] waypointDesc)
{
// Call the service
RouteInfo routeInfo = xRouteClient.calculateRouteInfo(waypointDesc, null, null, null);
// Create the result
TimeSpan t = TimeSpan.FromSeconds(routeInfo.time);
string[] result = new string[2];
result[0] = string.Format("{0} KM", routeInfo.distance);
result[1] = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
// Return the result
return result;
}
/* getRouteLines()
* Input: WaypointDesc[]
* Output: Route object
* Edited 20/12/12 - Davide Nguyen
*/
private static Route calculateRoute(WaypointDesc[] waypointDesc)
{
#region ResultListOptions
// Instantiate a new object resultListOPtions
ResultListOptions resultListOptions = new ResultListOptions();
resultListOptions.polygon = true;
resultListOptions.totalRectangle = true;
resultListOptions.detailLevel = DetailLevel.STANDARD;
#endregion
#region CallerContext/CallerContextPropery
// Create a new CallerContextProperty object property
xRoute.CallerContextProperty property = new xRoute.CallerContextProperty();
property.key = "ResponseGeometry";
property.value = "WKT";
xRoute.CallerContext callerContext = new xRoute.CallerContext();
callerContext.wrappedProperties = new xRoute.CallerContextProperty[] { property };
#endregion
// Call the service
Route route = xRouteClient.calculateRoute(waypointDesc, null, null, resultListOptions, callerContext);
return route;
}
}
}
这两者之间似乎存在一些参考混淆
using Plantool.xMap;
using Plantool.xRoute;
您可以删除以下内容:
using Plantool.xMap;
using Plantool.xLocate;
using Plantool.xRoute;
再加上:
using Plantool;
然后显式引用正确的类型,确保引用的是所需的类型。这将解决所有三条错误消息
理想情况下,应该避免名称冲突,因为只有名称空间不同。在.NET框架中,您会注意到他们会小心避免这种情况,例如
System.Data.Odbc.OdbcConnection
System.Data.SqlClient.SqlConnection
它们都可以被称为Connection
,因为一个在Odbc
命名空间中,另一个在SqlClient
命名空间中,但这可能会导致出现问题
通过将
LineString
重命名为RouteLineString
和MapLineString
可以避免应用程序中的混淆。根据我在xServer中的记忆,这是一个主要问题:xMap、xRoute和xLocate中的常见类型不一样(正如您所说,因为它们是可选模块)xMap.LineString
实际上是与xRoute.LineString
不同的类型,即使它们具有相同的属性。对此你无能为力,除了:
- 如果两种类型共享一个公共基类型或接口,请改用此公共类型
- 编写扩展方法以从
映射到xRoute.LineString
等xLocate.LineString
public static xLocate.LineString ToXLocateLineString(this xRoute.LineString lineString)
{
return new xLocate.LineString
{
// Map type by copying properties
....
};
}
这样你就可以写作了
xRoute.LineString[] input = ....
xMap.LineString[] output = input.Select(z => z.ToXLocateLineString()).ToArray();
甚至
public static xLocate.LineString[] ToXLocateLineStringArray(this xRoute.LineString[] lineString)
{
return lineString.Select(z => z.ToXLocateLineString()).ToArray();
}
然后
也许自从我上次使用API以来,他们改进了他们的API,但对此并不确定。干杯,在我看来,冗余可以通过正确创建客户端类来解决。您可以使用Visual Studio的WSDL.EXE在一个步骤中合并WSDL,而不是(通过Visual Studio Wizzard)逐个添加三个WSDL。这在ptvxserver1(在这个线程中被引用)中起作用,在xServer2中效果更好 我通常做的是一个CMD语句,比如 WSDL/名称空间:“XServer”/sharetypes/out:“XServer.cs”https://xserver2-europe-eu-test.cloud.ptvgroup.com/services/ws/XLocate?wsdl" "https://xserver2-europe-eu-test.cloud.ptvgroup.com/services/ws/XRoute?wsdl" "https://xserver2-europe-eu-test.cloud.ptvgroup.com/services/ws/XTour?wsdl"
将此视为一般建议。现在映射接口完全不同了。谢谢,这解决了这个问题,出现了关于共享方法的新接口。错误1无法将类型“Plantool.xRoute.Point”隐式转换为“Plantool.xMap.Point”错误2无法将类型“Plantool.xRoute.Point”隐式转换为“Plantool.xMap.Point”错误3无法将类型“Plantool.xRoute.LineString”隐式转换为“Plantool.xMap.LineString”代码保持不变,除了现在所有的东西都是用Plantool显式编码的;更改名称空间api名称不是一个选项,因为它是一个liscensed api。忘记添加了。如果事情变得非常棘手,您无法更改名称,您可以使用以下语句别名:
using Map=Plantool.xMap代码>让你使用<代码> map .ListSnc< /Cord>,谢谢,我会试试看。这是我现在手工做的,但是如果我的应用程序要求我重复同样的事情,我会考虑使用这个函数,谢谢。API文档只针对java,在C#所做的许多更改中都没有。比如setCoords=coords或者在某些上下文中是wrappedwords等等@DavideNguyen是的,他们的C#文档不是我读过的最好的文档。。。可以肯定的是,extensions方法可以帮助您编写那些无聊的映射程序。有关更多示例,请参见编辑的问题。
xMap.LineString[] output = input.ToXLocateLineStringArray();