I want to draw route between 2 points in Google map with a pin mark at ends points. in this, user can choose any driving route like by car, by cycle, by walk. I have to show routing information like "2 miles ahead , left from next street" and also estimated time.so regarding this , I found a lot of tutorial on the internet. the best I found is:
http://code.google.com/p/j2memaprouteprovider/source/browse/#svn/trunk/J2MEMapRouteAndroidEx
which is very complex and slow.
next I found this one:
http://www.anddev.org/google_driving_directions_-_mapview_overlayed-t826.html
this one is very similar to what I want, except some feature. but it is outdated.
I also found that I have to use KML for this. but i don't want to use it because of complexity so is it necessary to use KML? is there any better way to do this? can i have full source code as I got confused by seeing chunk of code. thanks a lot in advance,.
rock,
The simplest solution for this is to open "Google Map Application" that is already installed in android device. Just pass the source and target location's latitude and longitude as a parameter and call the google map application as follows:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+latitude_source+","+longitude_source+"&daddr="+latitude_dest+","+longitude_dest));
startActivity(i);
Using this approach, you can also choose any driving route like by car, by cycle, by walk etc and other functionalities that is provided by application.
Google Directions API contains everything you need. I found it easier to learn how the service works before looking at any complete source code.
Basically you will have to execute a UrlConnection and read the input stream. If you paste a basic request into a browser, you can see how your input stream would look like. Ex:
http://maps.googleapis.com/maps/api/directions/xml?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false
The result contains everything you would need but you will have to parse the result for information like step by step directions. Also the node called "overview_polyline" is what you would use to draw a complete route on the MapView. You could use the formula here to decode that polyline into a list of coordinates.
Related
In my app I am trying to visualize best route between two points. The route is calculated using data stored in my offline database. After some google search I have seen it is easy to visualize a route using KmlDocument. But all the example I found are Just showing how to create KmlDocument from kml file and visualize it. But in my situation it is not possible, as the route is generated dynamically. So how can I visualize the route on OSMdroid?
If you just want a line on the map that represents your route, then I'm pretty sure the class you want is Polyline.
Source: https://github.com/osmdroid/osmdroid/blob/master/osmdroid-android/src/main/java/org/osmdroid/views/overlay/Polyline.java
There a few different variants which have different effects depending on what you want, such as the Milestone classes.
Source: https://github.com/osmdroid/osmdroid/tree/master/osmdroid-android/src/main/java/org/osmdroid/views/overlay/milestones
Examples for both are here: https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/drawing/CustomPaintingSurface.java#L132
I'd suggest you get the sample app from the play store, play around with what's there, then find the source to what best fits your needs and go from there.
I have 2 coordinates (one of source and one of destination) , both of which keep changing every 5 seconds. I want to display path between these two points in android.
I saw this and this. The first one appeared to be easy,because small code :). But it was using javascript. The second one however is for android and is quite intimidating for a begginer like me (Also it has not been edited for quite some time).
Is there an easier way?
For routing between two coordinates you need to use online Apis. There is no Offline method.
Here is the Api :
http://maps.googleapis.com/maps/api/directions/json?language=fa&units=metric&mode=driving&origin=52.381215,4.9880833&destination=52.345784,4.832558
it return you the json data and its to you to show the route on map.
language : its for the language of step by step routing
origin : source point
destination : destination point
Alongside with my friends I am about to develop an android application. The application will use maps a lot, finding routes, placing markers, drawing predefined routes, stuff like that.
There is this Google Maps APIv2. So I tried it, write some code and looks good. I am able to place markers and draw polylines, however my application will have some predefined routes. One route is set of LatLng points. So I just add these points to PolylineOptions and add it to map. Great, it works.
But these predefined routes will be created in web-based application also using Google Maps APIv2 or similar api/library. Some streets are not straight they are curved so you need to create a lot of LatLng points to get it look nice which is very important. I can do this but I want to put only two LatLng points - at the beginning of the street and at the end.
Here is the image:
On the left, route has only two LatLng points (red dots) and that is desired shape of line. On the right same route with 2 LatLng points gives me route coloured in light blue, and this is what actually happens if I just add polyline to GoogleMap object. In order to create same effect as in left image, I need to create many LatLng (red dots) points which is not appropriate for those who will create these routes. So this leads us to routing problem which can be solved by using Google Directions API. Which solves the problem of drawing routes with less markers. Basically what I understood, this API for directions actually creates a lot of these LatLng points that I draw using for-each loop as seen on right part of picture above. And I need to contact Google Web Service for this so I need to have Internet connection - and this is the problem.
Sometimes internet will not be available hence I need some alternative solution. I've heard of Open Street Maps, but I am not sure how to use it and does it needs internet connection for creating these routes.
Is there any free library that offers offline routing and showing maps offline? Basically all map interactions must be done without use of internet.
But it is understandable if internet connection is needed only for the first run of application as it is needed for Google Maps APIv2, well application crashes until I turn on internet, after which any next run I do not need internet..
A web based application will be made for creating these routes and routes will be created by users for whom we develop this application. And as users can be very annoying they will demand some user-friendly and easy interface for creating routes. Telling them that they need to create 100 to 500 points for each route, and if there is 60-70 routes they will gladly say NO to our application.
Any advice? If it is possible, I can write my own routing algorithm for Google Maps to work offline, I am very skilful at "Algorithms and Data Structures" - any guides/tutorials for this? But I guess this map is just set of tiles, and does not have necessary information for finding route.... So please correct If I am mistaken for anything.
You can use the Scout SDK (provides a free usage quota) as it offers full offline maps, routing & navigation.
Have a look into the open source projects GraphHopper, Mapsforge and OpenScience-VTM which can do routing and/or maps offline. And all use OpenStreetMap data. Other projects exists of course, have a look here and here.
Also have a look into the GraphHopper map matching component which could be interesting for your specific use case and can work offline too.
(Note I'm the author of GraphHopper)
If it is possible, I can write my own routing algorithm for Google Maps to work offline
No you can't, the data is not open nor you can buy the data to my knowledge.
Another option is OsmAnd. The OSM wiki also has some information about OsmAnd and even more information about OSM on Android in general as well as pages about rendering and routing.
I need a Map API for Android that can provide me with indexed nodes and indices that make up the road network. The main idea is to determine if two GPS devices are on the same road. Thank you in advance
A Map API by itself will not have that information. Anyway, you can get it from OpenStreetMap freely. You can download it from here.
I don't understand from your question if you intend on displaying the results on a map. If so, and you want a nice and free map API, I would suggest Leaflet. It's not as mature as the likes of OpenLayers but, as you've tagged this post with "android", Leaflet just kicks ass in the mobile department.
OpenStreetMap is definitely a good source of data for this kind of project. Unlike google maps, it gives developers access to the underlying vector data of a map (fully open). This allows interesting new use cases which simply are not possible with google maps, and something involving geometric calculations like this would definitely fit into that category. You either need OpenStreetMap or some other source of "vector" map data, and beyond OpenStreetMap this can be expensive.
Unfortunately that's not the full answer to your question. You still have a lot of work to do to use the data in the way you intend. You need to calculate the proximity of two points (GPS readings from two devices?) to nearby roads, and figure out which road the point lies closest to. It's the kind of powerful geo calculation you might do using a GIS package such as QGIS or a functions of a geo-aware database system PostGIS.
But that's not the answer to your question, because you need to do these calculations on device. I'm not aware of an off-the-shelf library to do this on android. I think you would have to roll your own.
But another challenge is to get the vector data onto the device in a suitable format in the first place, and this is the first thing to solve. You'd want the vector data either as a large download for a whole country, or perhaps a smaller area, perhaps with an on-the-fly download feature within this app. Whole countries are not infeasible when working with maps in vector form (ever tried the awesome MayDroyd app?), but require some compact formatting. Happily some of these problems are starting to be solved in open source off-the-shelf libraries. You could try to build on top of MapsForge for example.
So then you're back to the challenge of writing on-device code to poke around in this data and do the calculations you want to do. I suppose it could be rather good if projects like MapsForge included generic PostGIS style geo-functions to make this easier. Something to ask the mapsforge developers about perhaps.
I am using following example source code but I can't get long distance route in map. It only works for small distance on map.
Is there are any limitation to access map?
If some one has better idea please help.
Here is a complete source code at https://github.com/frogermcs/RoutePathExample for how to draw path between two geolocation. I use this code without any trouble at all. You should check it
Make use of this secret Google API and Android Map API to show the distance between two points.
I am using this code and it is working fine, even if I put two points in two different countries. I believe you are not implementing it in the right way, because this code is retrieving the KML provided by Google for the two points you choose, which always returns a route unless the two points are in different continents and there is no route for going by car.
I suggest you show us how did you use the code in your app.