I have an array with positions. I wanted to parse this array to show polyline in google maps.
Here is an example:
how can I parse this array?
double [][] pos = {
{3.7067858,-14.4728779},
{3.7067858,-14.4728779},
{3.7067858,-14.4728779},
{3.7067858,-14.4728779},
{3.7067858,-14.4728779},
{3.7067858,-14.4728779},
{3.7067858,-14.4728779}};
PolylineOptions rectOptions = new PolylineOptions()
for (int i = 0; i < 1; i++){
.add(new LatLng(pos[i][i]))
i++;
}
Polyline polyline = mMap.addPolyline(rectOptions);
Building on Evan's answer, you can build your LatLng objects within your loop.
double[][] pos = { {3.7067858,-14.4728779}, ... };
PolylineOptions rectOptions = new PolylineOptions()
for (int i = 0; i < pos.length; i++){
rectOptions.add(new LatLng(pos[i][0], pos[i][1]));
}
Polyline polyline = mMap.addPolyline(rectOptions);
As a side note, a 2D array of doubles is fine for point lists. It is how LineStrings are defined in the GeoJSON specification.
First of all a 2d array for position data doesn't make much sense what you should do is have an array of LatLng's:
LatLng[] pos = {new LatLng(3.7067858,-14.4728779), ..., ...};
PolylineOptions rectOptions = new PolylineOptions();
for (LatLng l : pos){
rectOptions.add(l);
}
Polyline polyline = mMap.addPolyline(rectOptions);
Related
In a project I'm displaying a route using PolylineOptions for drawing rects from one POI to the next POI.
Also in each POI I'm drawing a circle.
The objective is to represent the direction of the route in some way, for example, drawing an arrow in the middle of each PolylineOptions rect. The problem is that I can't find how to do this.
This is my code:
PolylineOptions rectOptions = new PolylineOptions();
float[] prevHSV = new float[3];
Color.colorToHSV(getResources().getColor(R.color.colorPrimary), prevHSV);
rectOptions.color(Color.HSVToColor(255, prevHSV));
String[][] routeInformation = ((MyApplication)getApplication()).getLineInformation(line);
ArrayList<Double[]> routeStops = Util.getFullRouteFromLine(this, line);
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (int i=0; i<routeInformation.length; i++){
LatLng latlng = new LatLng(Double.parseDouble(routeInformation[i][0]),Double.parseDouble(routeInformation[i][1]));
builder.include(latlng);
mMap.addCircle(new CircleOptions().center(latlng).radius(15).strokeColor(Color.HSVToColor(255, prevHSV)).fillColor(Color.HSVToColor(255, prevHSV)).zIndex(7777));
}
for (Double[] pos : routeStops){
rectOptions.add(new LatLng(pos[0],pos[1])).width(5);
}
mMap.addPolyline(rectOptions);
Is there a easy way to represent the direction of a route?
Something similar to this, but this is for the web version of google maps, not for the android version: http://jsfiddle.net/nX8U8/2/
IMHO easiest way is to use flat "north-oriented" direction marker (arrow) like that:
created via vector drawable ic_arrow_up.xml:
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:viewportHeight="560"
android:viewportWidth="560"
android:height="24dp"
android:width="24dp"
>
<path android:fillColor="#0000FF"
android:pathData="M0,559.43C0,557.943 279.994,-0.561 280.458,0C282.014,1.884 560.512,559.569 559.999,559.776C559.665,559.911 496.562,532.823 419.77,499.581C419.77,499.581 280.15,439.14 280.15,439.14C280.15,439.14 140.756,499.57 140.756,499.57C64.089,532.807 1.056,560 0.681,560C0.307,560 0,559.743 0,559.43C0,559.43 0,559.43 0,559.43Z"
/>
</vector>
placed on each path polyline segment middle point with angle calculated on segment bearing. Both polyline segment middle and bearing you can determine via SphericalUtil class of Google Maps Android API Utility Library . Bearing can be found via SphericalUtil.computeHeading() with first and second points of segment as arguments and LatLng of segment middle point - via SphericalUtil.interpolate() also with first and second points of segment as arguments from and to and constant 0.5 (half) as fraction argument.
So, with source like this:
...
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
List<LatLng> sourcePoints = new ArrayList<>();
PolylineOptions polyLineOptions;
sourcePoints.add(new LatLng(-35.27801,149.12958));
sourcePoints.add(new LatLng(-35.28032,149.12907));
sourcePoints.add(new LatLng(-35.28099,149.12929));
sourcePoints.add(new LatLng(-35.28144,149.12984));
sourcePoints.add(new LatLng(-35.28194,149.13003));
sourcePoints.add(new LatLng(-35.28282,149.12956));
sourcePoints.add(new LatLng(-35.28302,149.12881));
sourcePoints.add(new LatLng(-35.28473,149.12836));
polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(sourcePoints);
polyLineOptions.width(10);
polyLineOptions.color(Color.BLUE);
mGoogleMap.addPolyline(polyLineOptions);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sourcePoints.get(0), 15));
for (int i = 0; i < sourcePoints.size() - 1; i++) {
// get first and second points of polyline segment
LatLng segmentP1 = sourcePoints.get(i);
LatLng segmentP2 = sourcePoints.get(i+1);
// calculate middle point
LatLng segmentMiddlePoint = SphericalUtil.interpolate(segmentP1, segmentP2, 0.5);
// calculate bearing
float segmentBearing = (float) SphericalUtil.computeHeading(segmentP1, segmentP2);
// add flat marker at segment middle
addDirectionMarker(segmentMiddlePoint, segmentBearing);
}
});
}
...
public void addDirectionMarker(LatLng latLng, float angle) {
Drawable circleDrawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_arrow_up);
BitmapDescriptor markerIcon = getMarkerIconFromDrawable(circleDrawable, 30, 30);
mGoogleMap.addMarker(new MarkerOptions()
.position(latLng)
.anchor(0.5f, 0.5f)
.rotation(angle)
.flat(true)
.icon(markerIcon)
);
}
you'll got something like that:
Also you can change size of arrows markers by set other values than 30, 30 in
BitmapDescriptor markerIcon = getMarkerIconFromDrawable(circleDrawable, 30, 30);
line.
Am working on an Android project where I want to draw route between 2 point on Google Map. I have successfully drawn route between source and destination point. But I have 1 problem in that, i.e. Some times I want to draw a path between more than 2 point, That time the code that I have written is drawing route between first and the last position and leaving the mid point position. What I exactly want is my route should go through the mid point to the destination point. How can I achieve this?
You can separately draw routes between three point. So, first draw the route from starting point to mid point then from mid point to end point. Then if you want add the required markers.
You can use PolylineOptions from GoogleMaps API Dcoumented Here and keep adding all the points which should be a part of your route. You can do something like this
ArrayList<LatLng> points;
PolylineOptions lineOptions = null;
// Traversing through all the routes
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<>();
lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(10);
lineOptions.color(Color.RED);
}
// Drawing polyline in the Google Map for the i-th route
if(lineOptions != null) {
mMap.addPolyline(lineOptions);
}
Hope this solves your problem.
I have a hicking app, that gets a array of markers from my database and plots them on my map, afterward it draws a polyline connecting all the markers. What i want to do is , to draw the polyline connecting the markers, but instead of showing 10 or 20 or 100 markers, to only SHOW 2, the marker that starts and the one that ends.
Would appreciate any help.
Below is the function that i use to draw the polylines.
private void createPolylinesFromLatLng(ArrayList<LatLng> polylist){
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int z = 0; z < polylist.size(); z++) {
LatLng point = polylist.get(z);
options.add(point);
totalDistanceInMeters = SphericalUtil.computeLength(polylist);
test=String.format("%.2f",totalDistanceInMeters);
}
and this is how i retrieve my markers into a array
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
JSONObject jo = array.getJSONObject(i);
Double lat = Double.parseDouble(jo.getString("lat"));
Double lng = Double.parseDouble(jo.getString("lon"));
polyline = new LatLng(Double.parseDouble(jo.getString("lat")),
Double.parseDouble(jo.getString("lon")));
polylist.add(polyline);
// location = new LatLng(lat,lng);
location=polyline;
MarkerOptions options = new MarkerOptions();
options.position(location);
mMap.addMarker(options);
thank you
One way of doing this would to just surround your code where you add the Marker with a if statement like this:
if(i == 0 || i == (array.length() - 1)){
MarkerOptions options = new MarkerOptions();
options.position(location);
mMap.addMarker(options);
}
That way only the first location and the last location are added.
I have 2 lists.
double pointY[]={105.45526,105.57364,105.53505,105.45523,105.51962,105.77320}
double pointX[]={9.99222,9.88347,9.84184,9.77197,9.55501,9.67768,9}
I want to make a List<LatLng> with LatLng[i] = (pointX[i], pointY[i])
I have the following code but it is not working:
List<LatLng> points;
for (int i = 0 ; i < pointX.length - 1; i++){
points[i].add(LatLng(pointX[i],pointY[i]));
};
How I can do it?
List<LatLng> points=new ArrayList<LatLng>();
for (int i = 0 ; i < pointX.length; i++){
points.add(new LatLng(pointX[i],pointY[i]));
};
Before adding data you just initialized ArrayList
List<LatLng> points=new ArrayList<LatLng>();
Firstly, you have seemingly failed to initialise the points variable - you will need to call the constructor new ArrayList<>().
You are also incorrectly adding to the list within the loop and not initialising a new LatLng within the loop for each new point you are trying to place in the list.
The following should work:
List<LatLng> points = new ArrayList<LatLng>();
for (int i = 0 ; i < pointX.length; i++){
points.add(new LatLng(pointX[i],pointY[i]));
}
You can then access the objects in the list as following:
//Some variable to determine which point you require
int x = 0;
LatLng latlng = points.get(x);
I am building an android application that gets the users current position and find nearby attractions. When I select an attraction a route is drawn to it from the current position but when I do this a second time the first route stays there, I want it to disappear. Below is the code I use to draw the line. Each time a direction is drawn this is called. I have tried to use line.remove each time before method is called but this removes both lines then. Any suggestions?
for (int i = 0; i < pontos.size() - 1; i++) {
LatLng src = pontos.get(i);
LatLng dest = pontos.get(i + 1);
try{
//here is where it will draw the polyline in your map
line = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.width(2).color(Color.RED).geodesic(true));
Save your Polylines in an array so you can remove them before other ones are added:
List<Polyline> mPolylines = new ArrayList<>();
private void someMethod() {
// Remove polylines from map
for (Polyline polyline : mPolylines) {
polyline.remove();
}
// Clear polyline array
mPolylines.clear();
for (int i = 0; i < pontos.size() - 1; i++) {
LatLng src = pontos.get(i);
LatLng dest = pontos.get(i + 1);
mPolylines.add(mMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.width(2).color(Color.RED).geodesic(true)));
}
}