Using PTV xServer internet in Java applications (xMap)
-
While xServer internet uses HTTP basic authentication with <user>/<password>, it is highly recommended to work with the token assigned to your subscription using the combination “xtok”/<token>. Using the token is a safer way than using your far more sensible account data.
-
-
Get the PTV Java Client Bundle
For this sample you are going to need the xMap client package and different third-party packages. Enter "xtok" as user and your token as password to access the download. Look at our xLocate sample to learn how to add the packages to your project.
-
Add the code to your project
This is a simple application which requests a map image and displays it on a window. Pay attention to the comments in the code!
package xMap_Sample;
import java.awt.Dimension;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.ptvag.jabba.core.exception.SystemException;
import com.ptvag.jabba.service.baseservices.CallerContext;
import com.ptvag.jabba.service.baseservices.CallerContextProperty;
import com.ptvag.jabba.service.baseservices.ClientFactory;
import com.ptvag.jabba.service.baseservices.RemoteType;
import com.ptvag.xserver.common.BoundingBox;
import com.ptvag.xserver.common.PlainPoint;
import com.ptvag.xserver.common.Point;
import com.ptvag.xserver.common.XServiceException;
import com.ptvag.xserver.xmap.ImageInfo;
import com.ptvag.xserver.xmap.Map;
import com.ptvag.xserver.xmap.MapParams;
import com.ptvag.xserver.xmap.MapSection;
import com.ptvag.xserver.xmap.XMapException;
import com.ptvag.xserver.xmap.XMapRemoteInterface;
public class xMapSample extends JPanel {
Dimension imageSize = new Dimension(240, 320);
JLabel label = new JLabel("Loading ...");
private void runSample() {
// Step 1:
// Create the client, setting username = "xtok", the required token and the url
XMapRemoteInterface client = (XMapRemoteInterface)ClientFactory.createClient(
XMapRemoteInterface.class, RemoteType.DOCSTYLE_CXF,
"xtok", "<INSERT-YOUR-TOKEN-HERE",
"https://xmap-eu-n-test.cloud.ptvgroup.com/xmap/ws/XMap"
);
// Step 2:
// Create object MapSection
MapSection mapSection = new MapSection();
Point point = new Point();
PlainPoint plainPoint = new PlainPoint();
double x = 682007.75;
double y = 6372537.96;
plainPoint.setX(x);
plainPoint.setY(y);
point.setPoint(plainPoint);
mapSection.setCenter(point);
mapSection.setScale(1000);
// Step 3:
// Create object CallerContext
CallerContext cxt = new CallerContext();
cxt.setLog1("Basic Mapping");
// Set CallerContext properties to define profile and CoordFormat
CallerContextProperty props1 = new CallerContextProperty();
props1.setKey("profile");
props1.setValue("default");
CallerContextProperty props2 = new CallerContextProperty();
props2.setKey("CoordFormat");
props2.setValue("PTV_MERCATOR");
CallerContextProperty[] properties = new CallerContextProperty[]{props1, props2};
cxt.setProperties(properties);
client.setCallerContext(cxt);
// Step 4:
// Create object ImageInfo
ImageInfo imageInfo = new ImageInfo();
imageInfo.setHeight(240);
imageInfo.setWidth(320);
imageInfo.setImageParameter("");
// Step 5:
// Create object MapParams
MapParams mapParams = new MapParams();
mapParams.setShowScale(true);
mapParams.setUseMiles(false);
// step 6:
// set attribute includeImageInResponse
boolean includeImageInResponse = false;
try {
// Step 7:
// Render map
Map map = client.renderMap(mapSection, mapParams, imageInfo, null, includeImageInResponse);
// Step 8:
// Retrieve further map values
BoundingBox bbox = map.getVisibleSection().getBoundingBox();
double leftTopX = bbox.getLeftTop().getPoint().getX();
double leftTopY = bbox.getLeftTop().getPoint().getY();
double rightBottomX = bbox.getLeftTop().getPoint().getX();
double rightBottomY = bbox.getLeftTop().getPoint().getY();
int scale = map.getVisibleSection().getScale();
System.out.println(String.format("Left top corner:(%.2f/%.2f)\nRight bottom corner:(%.2f/%.2f)\nMap scale: %s",
leftTopX, leftTopY, rightBottomX, rightBottomY, scale));
// Step 9:
// Get the image
String imageUrl = "http://" + map.getImage().getUrl();
try {
ImageIcon icon = new ImageIcon(new URL(imageUrl));
label.setIcon(icon);
label.setText("");
this.setMinimumSize(new Dimension(icon.getIconWidth() + 20, icon.getIconHeight() + 30));
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
} catch (SystemException se) {
System.err.println("Network or URL problem (xmap):\n" + se);
} catch (XMapException xme) {
System.err.println("Problem with calculating a map\n" + xme);
} catch (XServiceException xse) {
System.err.println("Technical problem on xMap server\n" + xse);
}
}
public xMapSample() {
this.add(label);
Thread thread = new Thread() {
public void run() {
runSample();
}
};
thread.start();
}
public static void main(String[] args) {
// Display image in window
JFrame frame = new JFrame("PTV xServer internet - Basic Mapping");
JPanel sample = new xMapSample();
frame.getContentPane().add(sample);
frame.setVisible(true);
frame.pack();
frame.setSize(340, 280);
// Exit when window is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
-
The output of this sample should now look like this: