How to add LatLng into List<LatLng> in Android? - android

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);

Related

I want to show distance on Polyline googlemap Android

I want to show distance of each connected node on polyline. Currently i am not getting such method to show the text over polyline. Please help is it possible to do in android google maps api.
This method (Java) adds a polyline to the map and adds the distance of each segment as text on the polyline - using the referenced method in another answer for displaying the text.
private void polylineLabels() {
LatLng pt1 = new LatLng(39.935484,-83.023490);
LatLng pt2 = new LatLng(39.098487,-84.446861);
LatLng pt3 = new LatLng(39.743435,-86.104479);
LatLng pt4 = new LatLng(38.202789,-85.739295);
LatLng pt5 = new LatLng(38.614276,-90.138124);
routePts.add(pt1);
routePts.add(pt2);
routePts.add(pt3);
routePts.add(pt4);
routePts.add(pt5);
for (int i = 1; i < routePts.size(); i++) {
Polyline line = mMap.addPolyline(
new PolylineOptions()
.add(routePts.get(i-1), routePts.get(i)).
width(5).color(Color.RED));
double d = SphericalUtil.computeDistanceBetween(routePts.get(i-1),routePts.get(i));
int dInt = (int)d;
String msg = Integer.toString(dInt)+"m";
LatLng midpt = SphericalUtil.interpolate(routePts.get(i-1),routePts.get(i), 0.5);
Marker m = addText(this, mMap, midpt, msg, 2, 18);
}
}
And the result:
You'll have to experiment with offset location to better position each label.
Here's the answer which defines the addText method: https://stackoverflow.com/a/30185289/2711811 .

Markers in android google maps json

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.

add an ArrayList of markers to a google map

I am trying to display multiple markers on a google map but I seem to only get the last one to display. The reason is that the markers get overwritten in a for loop. I have created an ArrayList that will store the markers but I don't how to add it to the google Map..
the polylines library contains a method called addAll that adds an arraylist of polylines but markers does not have such a thing..could anyone suggest a way to add multiple markers to the map?
public void mapForGPS(GoogleMap googleMap) {
mMap = googleMap;
double[] firstCoords;
double[] lastCoords;
double[] receivedDataArray;
double latitude;
double longitude;
double[] firstSetOfCoordinatesFromArray;
double[] lastSetOfCoordinatesFromArray;
double[] tempCoordinatesArray;
String[] tempDurationAndTimeArray;
String time;
GpsData gpsDataObj;
String[] dataForOneGreenDot;
String[] arrayOfGreenDots = new String[3];
// this will store coordinates for one iteration of a selected day
ArrayList<LatLng> coordList = new ArrayList<LatLng>();
ArrayList<String[]> arrayListOfGreenDots = new ArrayList<String[]>();
// clear polyline array that has coordinates form previous iteration
coordList.clear();
// clears all the markers on the map for the next iteration
mMap.clear();
// loop through JSON array of coordinates and assign data into a coordinates array with an
// object of Latitude and Longitude
for (int i = 0; i <= arrayOfGpsData.size() - 1; i++) {
gpsDataObj = arrayOfGpsData.get(i);
tempCoordinatesArray = gpsDataObj.getArrayOfDoubles();
tempDurationAndTimeArray = gpsDataObj.getArrayOfStrings();
latitude = tempCoordinatesArray[0];
longitude = tempCoordinatesArray[1];
coordList.add(new LatLng(latitude,longitude));
time = tempDurationAndTimeArray[0];
String latString = String.valueOf(latitude);
String longString = String.valueOf(longitude);
arrayOfGreenDots[0] = latString;
arrayOfGreenDots[1] = longString;
arrayOfGreenDots[2] = time;
arrayListOfGreenDots.add(arrayOfGreenDots);
}
// if there was a previous polyline on the map, clear it
if(polylineGPS != null){
polylineGPS.remove();
}
// add coordinates to a polylineGPS object
options = new PolylineOptions().addAll(coordList).width(5).color(Color.BLUE).geodesic(true);
polylineGPS = mMap.addPolyline(options);
// if user selected date in date-time picker then marker will point to
// first set of coordinates in the coordinatesArray and the map will zoom in on that point
if(!arrayOfGpsData.isEmpty())
{
double[] coordinatesFromFirstIndex = new double[2];
String[] timeAndDurationFromFirstIndex = new String[2];
double[] coordinatesFromLastIndex = new double[2];
String[] timeAndDurationFromLastIndex = new String[2];
GpsData objectDataFromFirstIndex = new GpsData(coordinatesFromFirstIndex, timeAndDurationFromFirstIndex);
GpsData objectDataFromLastIndex = new GpsData(coordinatesFromLastIndex, timeAndDurationFromLastIndex);
// get first set of coordinates from array as starting position
objectDataFromFirstIndex = arrayOfGpsData.get(0);
firstSetOfCoordinatesFromArray = objectDataFromFirstIndex.getArrayOfDoubles();
// get last set of coordinates from array as ending position
objectDataFromLastIndex = arrayOfGpsData.get(arrayOfGpsData.size() - 1);
lastSetOfCoordinatesFromArray = objectDataFromLastIndex.getArrayOfDoubles();
//timeAndDurationFromFirstIndex = objectDataFromFirstIndex.getArrayOfStrings();
// place marker for starting position
LatLng firstLocationPointOfDevice = new LatLng(firstSetOfCoordinatesFromArray[0], firstSetOfCoordinatesFromArray[1]);
mMap.addMarker(new MarkerOptions().position(firstLocationPointOfDevice).icon((BitmapDescriptorFactory.fromResource(R.drawable.blue_marker))).title("Starting Location"));
// place marker for ending position
LatLng lastLocationPointOfDevice = new LatLng(lastSetOfCoordinatesFromArray[0], lastSetOfCoordinatesFromArray[1]);
mMap.addMarker(new MarkerOptions().position(lastLocationPointOfDevice).icon(BitmapDescriptorFactory.fromResource(R.drawable.pink_marker)).title("Ending Location"));
// zoom in on first set of coordinates form coordinatesArray
mMap.moveCamera(CameraUpdateFactory.newLatLng(firstLocationPointOfDevice));
// camera zoom in position
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(firstLocationPointOfDevice, 10));
List<Marker> markers = new ArrayList<Marker>();
// adding markers to array list
for(int k = 0; k <= arrayListOfGreenDots.size() - 1; k++){
dataForOneGreenDot = arrayListOfGreenDots.get(k);
double latSingle = Double.parseDouble(dataForOneGreenDot[0]);
double longSingle = Double.parseDouble(dataForOneGreenDot[1]);
String timeSingle = dataForOneGreenDot[2];
//drawMarker(latSingle, longSingle, timeSingle);
LatLng greenDot = new LatLng(latSingle, longSingle);
marker = mMap.addMarker(new MarkerOptions().position(greenDot).icon(BitmapDescriptorFactory.fromResource(R.drawable.green_dot)).title(timeSingle));
markers.add(marker);
}
}
// if user did not select any date from date time picker, then by default the map will zoom on vancouver
else if(arrayOfGpsData.isEmpty())
{
// default location if no date is selected
LatLng defaultLocation = new LatLng(49.2827, -123.1207);
mMap.moveCamera(CameraUpdateFactory.newLatLng(defaultLocation));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(defaultLocation, 10));
}
}
I really don't know why this question got downvoted because it addresses a legit concern for which there was no immidiate answer.
I have found a solution though:
for (int i = 0; i <= arrayOfGpsData.size() - 1; i++) {
gpsDataObj = arrayOfGpsData.get(i);
tempCoordinatesArray = gpsDataObj.getArrayOfDoubles();
tempDurationAndTimeArray = gpsDataObj.getArrayOfStrings();
latitude = tempCoordinatesArray[0];
longitude = tempCoordinatesArray[1];
coordList.add(new LatLng(latitude,longitude));
time = tempDurationAndTimeArray[0];
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.green_dot_6px))
.title(time)
);
}
as I loop through my arrayOfGpsData and assign lat and long to my LatLng object, I add a marker to that LatLng right away with a time stamp.
I hope this helps someone in the future :D

How can I parse array in android to put values to polyline?

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);

Multiple Polygon On Map

how to make multiple polygon on Google Map I am using
List<lat long> data = new Array List<Lat Long>();
but its create only one polygon when we draw another then last polygon deleted so anyone please help me thanks in advance
You can define a common method to draw polygon on google maps like this:
public PolygonOptions addPolygon(ArrayList < LatLng > arg) {
LatLng[] data = arg.toArray();
PolygonOptions polygonOptions;
for (int i = 0; i <= data.length; i++) {
polygonOptions = new PolygonOptions();
polygonOptions.add(data[i], data[i + 1], data[i + 2])).strokeColor(Color.RED).strokeWidth(2);
polygonOptions.fillColor(Color.parseColor("#51000000"));
return polygonOptions;
}
}
and then add it to your GoogleMap like this:
yourGoogleMap.addPolygon(addPolygon(data));

Categories

Resources