I have 2 LatLng Lists
List<LatLng> PairOfLatLong = new ArrayList<LatLng>();
List<LatLng> FakePairOfLatLong = new ArrayList<LatLng>();
Adding LatLng Here
PairOfLatLong.add(pLatLng);
But In Somewhere i want to copy PairOfLatLong in FakePairOfLatLong
I try this way but error occur
FakePairOfLatLong= (ArrayList<LatLng>) PairOfLatLong.clone();
you could use
List<LatLng> FakePairOfLatLong = new ArrayList<LatLng>(PairOfLatLong);
or
FakePairOfLatLong.addAll(PairOfLatLong);
Related
I am creating an android app which shows distance and duration of two marker points in the Map. In the onCreate() I have written the following code:
In MapsActivity.java
private List<LatLng> getDirectionPolylines(List<RouteObject> routes){
List<LatLng> directionList = new ArrayList<LatLng>();
for(RouteObject route : routes){
List<LegsObject> legs = route.getLegs();
for(LegsObject leg : legs){
String routeDistance = leg.getDistance().getText();
String routeDuration = leg.getDuration().getText();
setRouteDistanceAndDuration(routeDistance, routeDuration);
List<StepsObject> steps = leg.getSteps();
for(StepsObject step : steps){
PolylineObject polyline = step.getPolyline();
String points = polyline.getPoints();
List<LatLng> singlePolyline = decodePoly(points);
for (LatLng direction : singlePolyline){
directionList.add(direction);
}
}
}
}
return directionList;
}
I am not clear how to calculate distance & duration in 'getText' in the code above. I was not able to see some APIs like Distancebetween() which is using LtnLtg as references.
Please suggest how to calculate the distance and duration values.
var lat = marker.getPosition().lat();
var lng = marker.getPosition().lng();
I would like to know how can I add for example the same marker into different array lists. I have let's say 5 array lists and 7 markers. I am adding the markers to the map, adding each marker to it's corresponding list and filter to display or hide the markers according to the list selected by the user. Everything works fine when the lists contain different markers, but when I have common markers in two lists the common marker will be kept only in the last list added.
List<Marker> firstList = new ArrayList<>();
List<Marker> secondList= new ArrayList<>();
List<Marker> thirdList= new ArrayList<>();
List<Marker> fourthList= new ArrayList<>();
List<Marker> fifthList= new ArrayList<>();
Marker marker1 = mMap.addMarker(new MarkerOptions().position(latLong1).title("MARKER1").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker1"));
Marker marker2 = mMap.addMarker(new MarkerOptions().position(latLong2).title("MARKER2").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker2"));
Marker marker3 = mMap.addMarker(new MarkerOptions().position(latLong3).title("MARKER3").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker3"));
Marker marker4 = mMap.addMarker(new MarkerOptions().position(latLong4).title("MARKER4").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker4"));
Marker marker5 = mMap.addMarker(new MarkerOptions().position(latLong5).title("MARKER5").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker5"));
Marker marker6 = mMap.addMarker(new MarkerOptions().position(latLong6).title("MARKER6").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker6"));
Marker marker7 = mMap.addMarker(new MarkerOptions().position(latLong7).title("MARKER7").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker7"));
firstList.add(marker1);
secondList.add(marker2);
secondList.add(marker3);
thirdList.add(marker1);
thirdList.add(marker4);
fourthList.add(marker5);
fifthList.add(marker6);
fifthList.add(marker7);
fifthList.add(marker1);
fifthList.add(marker2);
So when I filter the markers by lists for example if I filter for firstList no marker will be displayed, if I filter for thirdList only marker4 will be displayed and if I filter for fifthList than al the markers will be displayed including marker1. So the list keeps only the last added common items. So how can I make it so that the lists will keep all their objects even though they are common to other lists?
Thanks in advance!
UPDATE:
I don't know if this is exactly what you suggested, but I did this and now the filter is working properly. By doing this I have another problem.
List<Marker> firstList = new ArrayList<>();
List<Marker> secondList= new ArrayList<>();
List<Marker> thirdList= new ArrayList<>();
List<Marker> fourthList= new ArrayList<>();
List<Marker> fifthList= new ArrayList<>();
firstList.add(mMap.addMarker(new MarkerOptions().position(latLong1).title("MARKER1").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker1")));
secondList.add(mMap.addMarker(new MarkerOptions().position(latLong2).title("MARKER2").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker2")));
secondList.add(mMap.addMarker(new MarkerOptions().position(latLong3).title("MARKER3").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker3")));
thirdList.add(mMap.addMarker(new MarkerOptions().position(latLong1).title("MARKER1").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker1")));
thirdList.add(mMap.addMarker(new MarkerOptions().position(latLong4).title("MARKER4").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker4")));
fourthList.add(mMap.addMarker(new MarkerOptions().position(latLong5).title("MARKER5").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker5")));
fifthList.add(mMap.addMarker(new MarkerOptions().position(latLong6).title("MARKER6").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker6")));
fifthList.add(mMap.addMarker(new MarkerOptions().position(latLong7).title("MARKER7").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker7")));
fifthList.add(mMap.addMarker(new MarkerOptions().position(latLong1).title("MARKER1").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker1")));
fifthList.add(mMap.addMarker(new MarkerOptions().position(latLong2).title("MARKER2").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)).snippet("Snippet marker2")));
Now I want to add OnInfoWindowClickListener to my markers info window. Each marker must open a new activity. Before doing this modification I was adding all the markers to the a private Map allMarkersMap = new HashMap(); and I was adding every marker to the HashMap with allMarkersMap.put(marker1, Marker1.class);
GoogleMap.OnInfoWindowClickListener MyOnInfoWindowClickListener = new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Class cls = allMarkersMap.get(marker);
Intent intent = new Intent(MainActivity.this, cls );
startActivity(intent);
}
};
How can I do this now?
Your markers have the same references, that's what leads you to this behaviour.
Instead of creating and passing markers, you should create MarkerOptions and when you add a marker to your array list then create a new Marker. MarkerOptions is the object which keep the informations you want for your markers, that's the only object which should be instantiated once.
I have a checkboxlist where the user can select some routes then a reponse is being getting from the server. I have method gotoLocation to upadte the location of the markers as well to add a new marker in the map when a new one is being inserted into the table on the serverside with the same route.
I had problem with adding a new marker to the map before so when I inserted data for a new marker with the same selected route in my database table, the new inserted one was not added to the map. I stored the id and Marker in the HashMap before I solved this problem by storing the data of the marker as String in the HashMap in this case the new inserted data is being added to the map as marker.
Now in the Update block I need to convert this data latit,longit, rout_dirc to marker to remove the old one from the map and HashMap before updating its location. How can I do that in my case? How can I remove the old marker from the map since I dont have Marker in the HashMap now also I cant use marker.remove() to delete the old one?
I appreciate any help
Code:
public class Map extends FragmentActivity {
GoogleMap map;
static HashMap<Integer, String> markerMap = new HashMap<Integer, String>();
static String marker_string;
static Marker marker = null;
private void gotoLocation(int id, double lat, double lng,
String route_direct) {
final float zoom = 11;
LatLng ll = null;
if (markerMap.containsKey(id)) {
// Update the location.
marker_string = markerMap.get(id);
String[] marker_string_split = marker_string.split(",");
double latit = Double.parseDouble(marker_string_split[0]);
double longit = Double.parseDouble(marker_string_split[1]);
String rout_dirc = marker_string_split[2];
LatLng LL_2 = new LatLng(lat, lng);
MarkerOptions markerOpt2 = new MarkerOptions().title(route_direct)
.position(LL_2);
// This here doest work.
marker.remove();
// Remove from the HashMap tp add the new one.
markerMap.remove(id);
ll = new LatLng(lat, lng);
MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
.position(ll);
marker = map.addMarker(markerOpt);
String lat1 = Double.toString(lat);
String lng1 = Double.toString(lng);
String data = lat1 + "," + lng1 + "," + route_direct;
markerMap.put(id, data);
zoom();
} else {
// Add a new marker
String lat1 = Double.toString(lat);
String lng1 = Double.toString(lng);
String data = lat1 + "," + lng1 + "," + route_direct;
markerMap.put(id, data);
ll = new LatLng(lat, lng);
MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
.position(ll);
marker = map.addMarker(markerOpt);
zoom();
}
}
}
It seems like your marker in marker.remove(); is set to null and it won't do any good unless you have a reference of the markers you want to delete. If you want to remove all old markers, then you can try the following:
mMap = super.getMap();
map.clear();
This removes everything. Otherwise, when you add markers you should have a reference if you need to delete it later.
For example, your marker variable is:
Marker marker = map.addMarker(..);
Then you can remove it by marker.remove();
Hope this makes sense;
mapView.clear()
it will remove all markers in the map,
you can also refer the google official documentation
https://developers.google.com/maps/documentation/ios-sdk/marker
I'm currently developing an android google maps app help to get direction between 2 points on the map. I'm able to get the response from google maps service (Direction API) and draw polyline (Google Maps Android SDK) base on list of steps but the line is not snap to the road (curved line stick to road on the map), it's just straight line.
How can I draw polyline snap to road in Android app? I'm using Android Studio.
Here is my code to draw polyline.
void updateMapDirection() {
Polyline newPolyline;
PolylineOptions options = new PolylineOptions().width(3).color(Color.BLUE).geodesic(true);
LatLng latLng = new LatLng(mLegs.get(0).getmStartLocation().getmLatitude(),
mLegs.get(0).getmStartLocation().getmLongitude());
options.add(latLng);
for (WNStep i : mLegs.get(0).getmSteps()) {
latLng = new LatLng(i.getmEndLocation().getmLatitude(), i.getmEndLocation().getmLongitude());
options.add(latLng);
}
newPolyline = map.addPolyline(options);
}
Thanks for your help.
This is how you can do it:
Once you have your JSON object as "result". Parse it, Also decode the polyline in form of a List.
final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes
.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
List<LatLng> list = decodePoly(encodedString);
Finally add Polyline Option by looping over the list and setting the properties of your polyline corresponding to the road you want to show as retrieved from Direction API on Google Maps.
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int z = 0; z < list.size(); z++) {
LatLng point = list.get(z);
options.add(point);
}
line = myMap.addPolyline(options);
How to draw a polyline in google map for multiple latitude and longitude coordinates. I need to draw polyline for atleast 20 sets of latitude and longitude dynamically.
Adding multiple points in map using polyline and arraylist
ArrayList<LatLng> coordList = new ArrayList<LatLng>();
// Adding points to ArrayList
coordList.add(new LatLng(0, 0);
coordList.add(new LatLng(1, 1);
coordList.add(new LatLng(2, 2);
// etc...
// Find map fragment. This line work only with support library
GoogleMap gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
PolylineOptions polylineOptions = new PolylineOptions();
// Create polyline options with existing LatLng ArrayList
polylineOptions.addAll(coordList);
polylineOptions
.width(5)
.color(Color.RED);
// Adding multiple points in map using polyline and arraylist
gMap.addPolyline(polylineOptions);
Example from Android documentation:
GoogleMap map;
// ... get a map.
// Add a thin red line from London to New York.
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
.width(5)
.color(Color.RED));
Just call .add as many times as you need.
Just create a function to retrieve JSON polylines as this:
private ArrayList<LatLng> getPolylines(String jsonStr) {
// file exists, it is the first boot
if (jsonStr != null) {
// linea init
LatLng polyline;
// array list of lines init
ArrayList<LatLng> polylines = new ArrayList<LatLng>();
// get json array
JSONArray jsonArray = JSON.getJSONArray(jsonStr, "polytag");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonPolyline;
try {
jsonPolyline = jsonArray.getJSONObject(i);
polyline = new LatLng(Double.valueOf(jsonPolyline.getString("lat")),
Double.valueOf(jsonPolyline.getString("lon")));
polylines.add(polyline);
} catch (JSONException e) {
Log.d(TAG, "JSONException reading polylines: " + e.getMessage());
} catch (Exception e) {
Log.d(TAG, "Exception reading polylines: " + e.getMessage());
}
}
return polylines;
}
return null;
}
Then get this ArrayList and populate it in this way:
ArrayList<LatLng> polylines = getPolylines(linesJsonStr);
map.addPolyline(new PolylineOptions().addAll(polylines).width(2.0f).color(Color.RED));
Hope it helps!
You can iterate over the array of points and for each of the coordinates add a new LatLng to polyline options then after the loop add the polyline option to the polyline.
val from = LatLng(it.data[0].lat, it.data[0].lng)
val to = LatLng(it.data.last().lat, it.data.last().lng)
map.addMarker(MarkerOptions().position(from).title("pickup location"))
map.addMarker(MarkerOptions().position(to).title("destination location"))
map.animateCamera(CameraUpdateFactory.newLatLngZoom(from, 13f))
var polylineOptions = PolylineOptions()
for (i in it.data){
polylineOptions.add(LatLng(i.lat, i.lng))
}
polylineOptions.width(5f).color(Color.RED)
map.addPolyline(
polylineOptions
)