[Ask] about Nokia Map API - Java ME
hi, i'm arif..
I'm making a map in the mobile application. I use Nokia Maps API. but, there are some questions about the Nokia Maps API.
1. How to display Scale Bar and Compass directions in Nokia Map API?
2. Can I use the A * algorithm for searching a route in Nokia Maps API?
Thanks..:)
Re: [Ask] about Nokia Map API - Java ME
firstly please do observe the sections provided in this discussion baord, I moved this from the general discussions, and hoping that in future you would post these directly to corect places.
Secondly, there is no point on repeating same questions over and over again, you have asked this alrerady in: [url]http://www.developer.nokia.com/Community/Discussion/showthread.php?235559-How-to-Route-on-Nokia-Map-API[/url]
That thread did have pretty good answer for the routing, and for the scale & compass, there are no readymade APIs, so you would need to make those by yourself.
Re: [Ask] about Nokia Map API - Java ME
To create a scalebar you will have to write you own [B]MapComponent[/B] and add it to the map.
Since the map is a Mercator projection, t he length of the bar will vary with the location displayed as the map stretches latitudes closer to the poles. Something like the code below will get you started.
[CODE]
ScaleBarComponent scaleBar = new ScaleBarComponent()
map.addMapComponent(scaleBar);[/CODE]
[CODE]
public class ScaleBarComponent implements MapComponent {
private MapDisplay map;
private static final String ID = "ScaleBar";
private static final String VERSION = "1.0";
private int y_coord;
private int x_coord_start;
private int x_coord_end;
private String text = "";
final static int[] SCALE_IN_METRES = new int[]{5000000, 2000000, 1000000,
600000, 300000, 150000, 75000, 30000, 20000, 10000, 5000, 2000, 1000,
500, 250, 100, 50, 25};
final static String[] SCALE_IN_METRES_TEXT = new String[]{"5000km", "2000km", "1000km",
"600km", "300km", "150km", "75km", "30km", "20km", "10km", "5km", "2km", "1km",
"500m", "250m", "100m", "50m", "25m"};
/**
* Attaches a Map to the Map Component.
* @param map
*/
public void attach(MapDisplay map) {
this.map = map;
}
// from MapComponent
public void detach(MapDisplay map) {
this.map = null;
}
// from MapComponent
public String getId() {
return ID;
}
// from MapComponent
public String getVersion() {
return VERSION;
}
// from MapComponent
public EventListener getEventListener() {
return null;
}
/**
* When the map is updated, we need to recalculate the length of the bar.
* @param zoomChanged
*/
public void mapUpdated(boolean zoomChanged) {
x_coord_end = map.getWidth() - 20;
y_coord = map.getHeight() - 20;
double zoom = map.getZoomLevel();
zoom = (zoom >= map.getMinZoomLevel()) ? zoom : map.getMinZoomLevel();
if (zoom <= SCALE_IN_METRES.length - 1) {
GeoCoordinate end = map.pixelToGeo(new Point(x_coord_end, y_coord));
x_coord_start = x_coord_end - 10;
while (map.pixelToGeo(new Point(x_coord_start, y_coord)).distanceTo(end) < SCALE_IN_METRES[(int) zoom]) {
x_coord_start--;
}
text = SCALE_IN_METRES_TEXT[(int) zoom];
}
}
public void paint(Graphics g) {
//
g.drawLine(x_coord_start, y_coord, x_coord_end, y_coord);
g.drawLine(x_coord_start, y_coord + 2, x_coord_start, y_coord);
g.drawLine(x_coord_end, y_coord + 2, x_coord_end, y_coord);
g.drawString(
text, x_coord_end, y_coord, Graphics.BOTTOM | Graphics.
RIGHT);
}
}[/CODE]
You could do something similar for a Compass, just add an image over the map instead.
Re: [Ask] about Nokia Map API - Java ME
@ mr.jasfox: thanks for you information, but i have question.
[B]private static final String VERSION = "1.0";[/B]
what can only be used in version 1.0?
@ mr.symbianyucca: i'm sorry for my mistake put this thread and repeating same questions
I've read articles that you provide about routing.
but I am still confused and I have some questions
1. What algorithm is used for routing in Nokia Map API?
2. Can I replace the algorithm with the algorithm I want?
Thanks you :)
Re: [Ask] about Nokia Map API - Java ME
[QUOTE=Arif_Mubashir;894029]@ mr.jasfox: thanks for you information, but i have question.
[B]private static final String VERSION = "1.0";[/B]
what can only be used in version 1.0?
[/QUOTE]
Please check the code, the explanation is included in there.. (i.e. its only version number for the component)
[QUOTE=Arif_Mubashir;894029]
@ mr.symbianyucca: i'm sorry for my mistake put this thread and repeating same questions
I've read articles that you provide about routing.
but I am still confused and I have some questions
1. What algorithm is used for routing in Nokia Map API?
2. Can I replace the algorithm with the algorithm I want?
[/QUOTE]
In general the routing is done in server, so you can not change algorithms for it, anyway, you could check what kind of setting options he API offers. Its your interface for the service, so you are bound to the options it is offering.
Re: [Ask] about Nokia Map API - Java ME
[QUOTE=symbianyucca;894032]Please check the code, the explanation is included in there.. (i.e. its only version number for the component)[/QUOTE]
that mean, I can't use the Map API 0.5?
[QUOTE=symbianyucca;894032]In general the routing is done in server, so you can not change algorithms for it, anyway, you could check what kind of setting options he API offers. Its your interface for the service, so you are bound to the options it is offering.[/QUOTE]
What there is no way to change the routing algorithm?
I still hope there is still a way to change it. :(
thank you
Re: [Ask] about Nokia Map API - Java ME
[QUOTE=Arif_Mubashir;894043]that mean, I can't use the Map API 0.5?[/QUOTE]
You can use the component with any version of the API, as symbianyucca said, [B]getVersion()[/B] just returns the version of the component, and does define not which version of the API is in use.
In general you would be better off using the latest 1.0 version of the API as it incorporates new features, bug fixes and so on. If you look at this thread: [url]http://www.developer.nokia.com/Community/Discussion/showthread.php?235575-Map-API-Asha-and-Java-SDK-1.1[/url] as a workaround I would suggest you use 0.5 with the Asha 303 for now until a fix has been implemented, but hopefully the update will be available soon.
[QUOTE=Arif_Mubashir;894043]What there is no way to change the routing algorithm?
I still hope there is still a way to change it.[/QUOTE]
You request a route as follows:
[CODE]RoutingMode[] modes;
WaypointParameterList wpl = new WaypointParameterList();
RouteFactory.getInstance().createRouteRequest().calculateRoute(wpl, modes, this);[/CODE]
To alter the routing algorthm you need to alter the [B]mode[/B] look at the javadoc for full details of [B]RoutingType[/B], [B]TransportMode [/B]and [B]RoutingOptions [/B]constants.
[B]RoutingType[/B] covers things like fastest, shortest and economic
[B]TransportMode [/B] covers things like car, pedestrian and public transport
[B]RoutingOptions [/B] covers avoiding things like motorways or tunnels.
Hopefully this should give you sufficient flexibility to get the route you want.
You could of course manipulate the route you receive after making the request, or alternatively add more [B]Waypoints[/B] to your journey to force the route to go via certain locations.
Re: [Ask] about Nokia Map API - Java ME
[QUOTE=jasfox;894054]
You can use the component with any version of the API, as symbianyucca said, getVersion() just returns the version of the component, and does define not which version of the API is in use.
In general you would be better off using the latest 1.0 version of the API as it incorporates new features, bug fixes and so on. If you look at this thread: [url]http://www.developer.nokia.com/Commu...d-Java-SDK-1.1[/url] as a workaround I would suggest you use 0.5 with the Asha 303 for now until a fix has been implemented, but hopefully the update will be available soon.
[/QUOTE]
Thanks for your information. :)
[QUOTE=jasfox;894054]
You request a route as follows:
[CODE]RoutingMode[] modes;
WaypointParameterList wpl = new WaypointParameterList();
RouteFactory.getInstance().createRouteRequest().calculateRoute(wpl, modes, this);[/CODE]
To alter the routing algorthm you need to alter the [B]mode[/B] look at the javadoc for full details of [B]RoutingType[/B], [B]TransportMode [/B]and [B]RoutingOptions [/B]constants.
[B]RoutingType[/B] covers things like fastest, shortest and economic
[B]TransportMode [/B] covers things like car, pedestrian and public transport
[B]RoutingOptions [/B] covers avoiding things like motorways or tunnels.
Hopefully this should give you sufficient flexibility to get the route you want.
You could of course manipulate the route you receive after making the request, or alternatively add more [B]Waypoints[/B] to your journey to force the route to go via certain locations.[/QUOTE]
that means, I can use my own algorithm to do the routing? such Dijstra or A * Search etc.?
Re: [Ask] about Nokia Map API - Java ME
[QUOTE=Arif_Mubashir;894079]
that means, I can use my own algorithm to do the routing? such Dijstra or A * Search etc.?[/QUOTE]
The API does not [B]"do" [/B]the routing itself as the API does not have an on board routing algorthm. Instead it makes requests to a routing [B]service[/B]. Calculating routes is a complex business, and the algorithms involved would be too much for a S40 device to handle. The API gives you the ability to add restrictions to your request so that that the routing service refines its response to meet your criteria.
Re: [Ask] about Nokia Map API - Java ME
[QUOTE=jasfox;894083]The API does not [B]"do" [/B]the routing itself as the API does not have an on board routing algorthm. Instead it makes requests to a routing [B]service[/B]. Calculating routes is a complex business, and the algorithms involved would be too much for a S40 device to handle. The API gives you the ability to add restrictions to your request so that that the routing service refines its response to meet your criteria.[/QUOTE]
I want to know what algorithm to use Nokia Map 0.5 API for routing?
Thanks
Re: [Ask] about Nokia Map API - Java ME
[QUOTE=jasfox;894054]
[CODE]RoutingMode[] modes;
WaypointParameterList wpl = new WaypointParameterList();
[B]RouteFactory[/B].getInstance().createRouteRequest().calculateRoute(wpl, modes, this);[/CODE]
[/QUOTE]
I use the Nokia Map API 0.5
But RouteFactory class was not found.
What is the Nokia Map 0.5 API does not support RoutingFactory class?
Thanks
Re: [Ask] about Nokia Map API - Java ME
I make the routing using the Nokia Map API 0.5
but routing did not work.
This code that I created
[CODE]
public class mnRute implements CommandListener {
mnUtm midlet;
Display display;
skalaMap skala;
legendaMap kompas;
MapCanvas mapCanvas;
MapDisplay mapDisplay;
Command cmTujuan, cmRute, cmHapus, cmKembali;
LocationProvider lp;
MapStandardMarker awal,akhir;
WaypointParameterList wpl;
Route[] routes;
MapFactory mapFactory;
public mnRute(mnUtm midlet, Display display) {
this.midlet = midlet;
this.display = display;
wpl = new WaypointParameterList();
skala = new skalaMap();
kompas = new legendaMap();
}
public void lokasiAwal() {
try {
//MAKE A MAP CANVAS
mapCanvas = new MapCanvas(display);
mapDisplay = mapCanvas.getMapDisplay();
//Membuat Command
cmKembali = new Command("Kembali", Command.BACK, 0);
cmTujuan = new Command("Pilih Tujuan", Command.SCREEN, 1);
cmHapus = new Command("Hapus Tujuan", Command.SCREEN, 2);
cmRute = new Command("Cari Rute", Command.SCREEN, 3);
mapCanvas.addCommand(cmKembali);
mapCanvas.addCommand(cmTujuan);
mapCanvas.addCommand(cmHapus);
mapCanvas.addCommand(cmRute);
mapCanvas.setCommandListener(this);
//Menentukan Posisi Awal
Criteria cr = new Criteria();
cr.setHorizontalAccuracy(500);
lp = LocationProvider.getInstance(cr);
Location l = lp.getLocation(60);
Coordinates c = l.getQualifiedCoordinates();
if (c != null) {
double lat = c.getLatitude();
double lon = c.getLongitude();
mapCanvas.getMapDisplay().setCenter(new GeoCoordinate(lat, lon, 0));
mapCanvas.getMapDisplay().setZoomLevel(16,0,0);
//MAKE CURRENT POSITION
Point center = new Point(mapCanvas.getWidth()/2, mapCanvas.getHeight()/2);
GeoCoordinate gc = mapDisplay.pixelToGeo(center);
wpl.addCoordinate(gc);
awal = mapCanvas.getMapFactory().createStandardMarker(gc, 10, null, MapShapeType.bed);
mapDisplay.addMapObject(awal);
mapDisplay.addMapComponent(skala);
mapDisplay.addMapComponent(kompas);
//Menampilkan Map
display.setCurrent(mapCanvas);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void lokasiTujuan() {
//MAKE A DESTINATION POINT
Point center = new Point(mapCanvas.getWidth() / 2, mapCanvas.getHeight() / 2);
GeoCoordinate gc = mapDisplay.pixelToGeo(center);
wpl.addCoordinate(gc);
akhir = mapCanvas.getMapFactory().createStandardMarker(gc, 10, null, MapShapeType.bed);
mapDisplay.addMapObject(akhir);
//mapCanvas.removeCommand(cmTujuan);
}
//CALCULATE ROUTE
public void perhitungan(RoutingMode[] modes) {
RouteManager rm = RouteManager.getInstance();
rm.calculateRoute(wpl, modes);
mapDisplay.removeAllMapObjects();
routes = rm.getRoutes();
for (int i = 0; i < routes.length; i++) {
mapDisplay.addMapObject(mapFactory.createMapPolyline(routes[i].getShape()));
mapDisplay.setCenter(routes[i].getStart().getMappedPosition());
}
}
//EXECUTE ROUTING
private void perutean(RoutingMode[] modes) {
perhitungan(modes);
}
public void commandAction(Command c, Displayable d) {
//Kembali ke Menu Utama
if (c == cmKembali) {
display.setCurrent(midlet.menu);
}
//Melakukan Penandaan Lokasi Tujuan
if (c == cmTujuan) {
lokasiTujuan();
}
if (c == cmRute) {
perutean(new RoutingMode[] { new RoutingMode() });
}
if (c == cmHapus) {
mapDisplay.removeAllMapObjects();
}
}
}
[/CODE]
I make a routing application with the current position as the starting position.
errors in
[CODE]
//CALCULATE ROUTE
public void perhitungan(RoutingMode[] modes) {
RouteManager rm = RouteManager.getInstance();
rm.calculateRoute(wpl, modes);
mapDisplay.removeAllMapObjects();
routes = rm.getRoutes();
for (int i = 0; i < routes.length; i++) {
[B] mapDisplay.addMapObject(mapFactory.createMapPolyline(routes[i].getShape()));[/B]
mapDisplay.setCenter(routes[i].getStart().getMappedPosition());
}
[/CODE]
warning:
java.lang.NullPointerException
-----------------------------------------------------
how to display the total distance between two points?
I hope your help, please
Thanks :)
Re: [Ask] about Nokia Map API - Java ME
1) Your [B]NullPointerException [/B]is due to the fact that your [B]mapFactory[/B] object has not been initialised.
Either:
Alter mnRute to Extend [B]MapCanvas [/B]so that the protected [B]mapFactory [/B]can be accessed from the base class.
Or:
create your own map factory using the static factory method : [B]MapFactory.createMapFactory(int tileResolution, int width, int height)[/B]
2) To obtain the length of a route use: [B]Route.getSummary().getDistance()[/B], to get an "as-the-crow-flies" distance you can [B]Route.getStart().getMappedPosition().distanceTo(Route.getEnd().getMappedPosition())[/B]
3) Nokia Map 0.5 is a beta release and there is no guarantee that the method signatures will be forward compatible. Hence the 'old' jar will not work with code written for the 1.x API. The method signatures of all associated location base service calls have been standardized from v 1.0 onwards.
If you must use the old jar, there is a routing example bundled with the old 0.5 zip. I suggest you have a look at the routing demo code as a basis for your example to get it working.
Re: [Ask] about Nokia Map API - Java ME
[QUOTE=Arif_Mubashir;893486]
How to display Scale Bar and Compass directions in Nokia Map API?
[/QUOTE]
An article on how to do this has now been submitted to the Community Wiki: [url]http://www.developer.nokia.com/Community/Wiki/Creating_Touchable_Custom_Map_Components_for_the_Maps_API_for_Java_ME#ScaleBarComponent[/url]
Re: [Ask] about Nokia Map API - Java ME
[QUOTE=jasfox;894301]
Either:
Alter mnRute to Extend [B]MapCanvas [/B]so that the protected [B]mapFactory [/B]can be accessed from the base class.
[/QUOTE]
what is the base class sir?
i extend MapCanvas :
[CODE]
public class mnRute extends MapCanvas implements CommandListener {
mnUtm midlet;
//Display display;
skalaMap skala;
legendaMap kompas;
MapCanvas mapCanvas;
MapDisplay mapDisplay;
Command cmTujuan, cmRute, cmHapus, cmKembali;
LocationProvider lp;
MapStandardMarker awal,akhir;
WaypointParameterList wpl;
Route[] routes;
//MapFactory mapFactory;
public mnRute(mnUtm midlet, Display display){
super(display);
wpl = new WaypointParameterList();
skala = new skalaMap();
kompas = new legendaMap();
this.midlet = midlet;
this.display = display;
}
public void lokasiAwal() {
try {
//Create map Canvas
//Membuat Canvas Map
mapCanvas = new MapCanvas(display);
mapDisplay = mapCanvas.getMapDisplay();
//Create Command
//Membuat Command
cmKembali = new Command("Kembali", Command.BACK, 0);
cmTujuan = new Command("Pilih Tujuan", Command.SCREEN, 1);
cmHapus = new Command("Hapus Tujuan", Command.SCREEN, 2);
cmRute = new Command("Cari Rute", Command.SCREEN, 3);
mapCanvas.addCommand(cmKembali);
mapCanvas.addCommand(cmTujuan);
//mapCanvas.addCommand(cmHapus);
//mapCanvas.addCommand(cmRute);
mapCanvas.setCommandListener(this);
//Start position with current position
//Menentukan Posisi Awal
Criteria cr = new Criteria();
cr.setHorizontalAccuracy(500);
lp = LocationProvider.getInstance(cr);
Location l = lp.getLocation(60);
Coordinates c = l.getQualifiedCoordinates();
if (c != null) {
double lat = c.getLatitude();
double lon = c.getLongitude();
mapCanvas.getMapDisplay().setCenter(new GeoCoordinate(lat, lon, 0));
mapCanvas.getMapDisplay().setZoomLevel(16,0,0);
//Make end position marker
//Membuat Tanda Posisi Awal di Map
Point center = new Point(mapCanvas.getWidth()/2, mapCanvas.getHeight()/2);
GeoCoordinate gc = mapDisplay.pixelToGeo(center);
wpl.addCoordinate(gc);
awal = mapCanvas.getMapFactory().createStandardMarker(gc, 10, null, MapShapeType.bed);
mapDisplay.addMapObject(awal);
mapDisplay.addMapComponent(skala);
mapDisplay.addMapComponent(kompas);
//displaying map
//Menampilkan Map
display.setCurrent(mapCanvas);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
//Make end position marker
public void lokasiTujuan() {
//Membuat Penanda Posisi Akhir
Point center = new Point(mapCanvas.getWidth() / 2, mapCanvas.getHeight() / 2);
GeoCoordinate gc = mapDisplay.pixelToGeo(center);
wpl.addCoordinate(gc);
akhir = mapCanvas.getMapFactory().createStandardMarker(gc, 10, null, MapShapeType.bed);
mapDisplay.addMapObject(akhir);
mapCanvas.removeCommand(cmTujuan);
}
//Calculate Route
public void perhitungan(RoutingMode[] modes) {
RouteManager rm = RouteManager.getInstance();
rm.calculateRoute(wpl, modes);
//mapDisplay.removeAllMapObjects();
routes = rm.getRoutes();
for (int i = 0; i < routes.length; i++) {
mapDisplay.addMapObject(mapFactory.createMapPolyline(routes[i].getShape()));
mapDisplay.setCenter(routes[i].getStart().getMappedPosition());
}
}
private void perutean(RoutingMode[] modes) {
perhitungan(modes);
}
public void commandAction(Command c, Displayable d) {
//Kembali ke Menu Utama
if (c == cmKembali) {
display.setCurrent(midlet.menu);
}
//Melakukan Penandaan Lokasi Tujuan
//Marking end position
if (c == cmTujuan) {
lokasiTujuan();
mapCanvas.addCommand(cmTujuan);
mapCanvas.addCommand(cmHapus);
mapCanvas.addCommand(cmRute);
}
//Execute Route
if (c == cmRute) {
perutean(new RoutingMode[] { new RoutingMode() });
}
if (c == cmHapus) {
//lokasiAwal();
mapDisplay.removeAllMapObjects();
mapCanvas.addCommand(cmTujuan);
}
}
}
[/CODE]
polyline is not display and no warning error
why the polyline is not display?
Thanks sir..