Here's some sample code from my guru Oliver - it is based on xServer.NET and you can use it both for
- PTV Developer - in this case ensure to provide a valid API key in line 37.
- XMap2 (I went for a local url)
What is also important to be aware of:
Oliver did choose a specific initialization sequence. Keep it!
WIth this approach the application needs to be "[StaThread]" (whatever that means
)
Best regards and many thanks to Oliver.
Code: Select all
using Ptv.XServer.Controls.Map;
using Ptv.XServer.Controls.Map.Layers.Shapes;
using Ptv.XServer.Controls.Map.Layers.Tiled;
using Ptv.XServer.Controls.Map.Symbols;
using Ptv.XServer.Controls.Map.TileProviders;
using Ptv.XServer.Controls.Map.Tools;
using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace GenerateMap
{
class Program
{
[STAThread]
static void Main(string[] args)
{
Point location = new Point(8.403951, 49.00921);
var mapView = new MapView();
mapView.Name = "Map";
var p = GeoTransform.WGSToPtvMercator(location);
mapView.SetXYZ(p.X, p.Y, 12, false);
mapView.Printing = true;
var sz = new Size(600, 400);
var apiKey = Properties.Settings.Default.DeveloperApiKey;
var basel = new TiledLayer("Road")
{
TiledProvider = new RemoteTiledProvider
{
MinZoom = 0,
MaxZoom = 22,
RequestBuilderDelegate = (x, y, z) =>
$"https://api.myptv.com/rastermaps/v1/image-tiles/{z}/{x}/{y}?style=silkysand&apiKey={apiKey}",
//$"http://127.0.0.1:50000/services/rest/XMap/tile/{z}/{x}/{y}",
},
IsBaseMapLayer = true,
};
var pin = new Pin
{
Color = Colors.Green,
Width = 40,
Height = 40,
};
ShapeCanvas.SetAnchor(pin, LocationAnchor.RightBottom);
ShapeCanvas.SetLocation(pin, location);
var myLayer = new ShapeLayer("MyLayer");
myLayer.Shapes.Add(pin);
basel.AddToMapView(mapView);
myLayer.AddToMapView(mapView);
mapView.Measure(sz);
mapView.Arrange(new Rect(new Point(0, 0), sz));
mapView.UpdateLayout();
var renderTargetBitmap = new RenderTargetBitmap((int)sz.Width, (int)sz.Height, 96d, 96d, PixelFormats.Default);
renderTargetBitmap.Render(mapView);
var jpegBitmapEncoder = new JpegBitmapEncoder();
jpegBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
var ms = new MemoryStream();
jpegBitmapEncoder.Save(ms);
ms.Close();
var bytes = ms.ToArray();
var fileName = Path.GetTempPath() + "\\Img" + Guid.NewGuid() + ".jpg";
File.WriteAllBytes(fileName, bytes);
// open the image
System.Diagnostics.Process.Start(fileName);
}
}
}