Markers in android google maps json - android

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.

Related

How to draw route between multiple LatLong Android map?

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.

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

google maps android, all markers being loaded within circle

I am using a circle and trying to use its latitude and longitude values to load markers from a database that are within distance, the problem is, it is loading all the markers, rather than just the ones within the circle.
Can anyone point out where I'm going wrong? Code:
float[] distance = new float[10];
for(int i=0; i<jArray.length(); i++)
{
JSONObject jsonData = jArray.getJSONObject(i);
databaseMarkerData.add(new BasicNameValuePair("name", jsonData.getString("name")));
databaseMarkerData.add(new BasicNameValuePair("address", jsonData.getString("address")));
databaseMarkerData.add(new BasicNameValuePair("description", jsonData.getString("description")));
databaseMarkerData.add(new BasicNameValuePair("rating", jsonData.getString("rating")));
databaseMarkerData.add(new BasicNameValuePair("lat", jsonData.getString("lat")));
databaseMarkerData.add(new BasicNameValuePair("lng", jsonData.getString("lng")));
databaseMarkerData.add(new BasicNameValuePair("estType", jsonData.getString("estType")));
databaseMarkerData.add(new BasicNameValuePair("reliability", jsonData.getString("reliability")));
String latText = jsonData.getString("lat");
Double lat = Double.parseDouble(latText);
String lngText = jsonData.getString("lng");
Double lng = Double.parseDouble(lngText);
getMarkerDataArray.add(new MarkerData(jsonData.getString("name"), jsonData.getString("address"), jsonData.getString("description"),
jsonData.getString("rating"), jsonData.getString("reliability"), lat, lng, jsonData.getString("estType")));
Location.distanceBetween( lat, lng, circle.getCenter().latitude, circle.getCenter().longitude, distance);
if( distance[i] < circle.getRadius())
{
plotMarkers(getMarkerDataArray);
}
}
this is my plotMarkers function just in case it is needed:
plotMarkers()
private void plotMarkers(ArrayList<MarkerData> markers)
{
if(markers.size() > 0)
{
for (MarkerData markerData : markers)
{
// Create user marker with custom icon and other options
MarkerOptions markerOption = new MarkerOptions().position(new LatLng(markerData.getLat(), markerData.getLng()));
Marker currentMarker = googleMap.addMarker(markerOption);
markerDataHashmap.put(currentMarker, markerData);
googleMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
There are more than one problem.
One is the first element of distance is what you should use:
if( distance[0] < circle.getRadius()){
plotMarkers(getMarkerDataArray);
}
And you just need one element array float[] distance = new float[1];
Two is this
if( distance[i] < circle.getRadius())
{
plotMarkers(getMarkerDataArray);
}
that getMarkerDataArray is a list that is filled regardless whether the distance condition is met or not and used in plotMarkers for every time it is. I think what you want is
for (...){
if( distance[0] < circle.getRadius()){
getMarkerDataArray.add(new MarkerData(...));
}
}
plotMarkers(getMarkerDataArray);

google maps marker array?

Hi guys and girls if any :))
this piece of code creates a null pointer exception
I would like to put a couple of markers in MO
LAT = Double.parseDouble(PartA);
LNG = Double.parseDouble(PartB);
LatLng Standort = new LatLng(LAT, LNG);
MO[y] = mMap.addMarker(new MarkerOptions()
.position(Standort)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.kreis)));
builder.include(MO[y].getPosition());
Array type expected - found com.google.android.gms.maps.model.maker
Definition private static Marker MOrt = null;
I don't know my answer help you out or not.
First of all declare array , than add points and for adding marker i just start a loop with add marker function.
LatLng[] point_new = new LatLng[3];
point_new[0] = new LatLng(24.8926596, 67.0835093);
point_new[1] = new LatLng(48.85837,2.294481);
point_new[2] = new LatLng(0, 0);
for (int i = 0; i < point_new.length; i++) {
drawMarker(point_new[i]);
}
//drawMarker method
private void drawMarker(LatLng point) {
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
// Adding marker on the Google Map
map.addMarker(markerOptions);
}
final output
hope this will help!
Happy coding

Center android google map on group of markers?

I have a bunch of markers in which I load onto a google map. My problem is that, the camera is not ontop of the markers. Can I center it on top of all my markers?
I am adding my markers with this code:
for(int i = 0; i < jsonArray.length(); i++) {
String breweryName = jsonArray.getJSONObject(i).getString("brewery");
String lat = jsonArray.getJSONObject(i).getString("lat");
String lng = jsonArray.getJSONObject(i).getString("lng");
String bID = jsonArray.getJSONObject(i).getString("breweryID");
double latD = Double.parseDouble(lat);
double lngD = Double.parseDouble(lng);
m.addMarker(new MarkerOptions()
.position(new LatLng(latD, lngD))
.title(breweryName));
}
I looked on stack and found this question, which is the same as mine:
Android Google maps API V2 center markers
But there isnt much context behind it and am not sure how to implement it.
You can animate your camera within specific bounds. Let's say you have an ArrayList of markers, something like (this could also be a list of LatLngs, doubles, anything that contains the latitude/longitude for your marker)
List<MarkerOptions> markers = new ArrayList<MarkerOptions>();
You can then make sure they are all visible on your map by doing the following
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLng position;
for(int i = 0; i < markers.size(); i++){
position = markers.get(i).getPosition();
builder.include(new LatLng(position.latitude, position.longitude));
}
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));

Categories

Resources