Drawing on map Api V2 with user movement - android

I want to draw the lines ( path ) on the map at run time, when user start moving in any direction.
Current now I can make a line between the two points statically like this -
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
md = new GMapV2Direction();
mMap = ((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng coordinates = new LatLng(13.685400079263206, 100.537133384495975);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 16));
mMap.addMarker(new MarkerOptions().position(fromPosition).title("Start"));
mMap.addMarker(new MarkerOptions().position(toPosition).title("End"));
Document doc = md.getDocument(fromPosition, toPosition, GMapV2Direction.MODE_WALKING);
int duration = md.getDurationValue(doc);
String distance = md.getDistanceText(doc);
String start_address = md.getStartAddress(doc);
String copy_right = md.getCopyRights(doc);
ArrayList<LatLng> directionPoint = md.getDirection(doc);
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
}
mMap.addPolyline(rectLine);
But I want to draw the line dynamically with the users movemement, is it possible with Google Maps Api V2 ?
Thanks

You can do this by using the function onMyLocationChange but your activity should implements OnMyLocationChangeListener
#Override
public void onMyLocationChange(Location arg0) {
lastLatLng = currentLatLng ;
currentLatLng = new LatLng(arg0.getLatitude(), arg0.getLongitude());
// here draw again path between those two points
}
now you have two new points the one the user currently exist , and the last point from the last location update, u will have to draw a new path without removing the old one so this will solve your problem i guess.

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 .

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 to make a map zoom in and center to specific lat/lang points?

I am trying to make it so that when I launch my app the phone will zoom in and center to the area in which I have set PolyLines to via an ArrayList of lat and lang points.
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
ArrayList<MyPoint> points = Data.getPoints();
PolylineOptions polylineOptions = new PolylineOptions();
for (int i = 0; i < points.size(); i++) {
MyPoint point = points.get(i);
polylineOptions.add(new LatLng(point.getLat(), point.getLng()));
}
Polyline polyline = mMap.addPolyline(polylineOptions);
}
I start up the app in genymotion and it zooms in and then centers at the lat/lang ArrayList points I've set.
Currently, I just get a map that shows nearly the whole globe and I have to zoom in manually to the polylines I've set up.
You need the camera update factory class to create a camera update and move the map's camera. Camera Update Factory Documentation
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
ArrayList<MyPoint> points = Data.getPoints();
int padding = 20; // or prefer padding
LatLngBounds.Builder builder = new LatLngBounds.Builder();
PolylineOptions polylineOptions = new PolylineOptions();
for (int i = 0; i < points.size(); i++) {
MyPoint point = points.get(i);
polylineOptions.add(new LatLng(point.getLat(), point.getLng()));
builder.include(new LatLng(point.getLat(), point.getLng()));
}
Polyline polyline = mMap.addPolyline(polylineOptions);
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
}
You need to use LatLngBounds and then zoom your camera to cover all the included markers:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
Use the following line in a loop for all points/markers: (You can use the same loop you are using to add points to polylineOptions.)
builder.include(pointLatLng);
And finally, (outside the loop) -
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));

How to create multiple markers with infoWindow showing google maps api

I'm using google directions and make a route between two points and setting a marker and I need to set a window with some information in both of these markers, but According to google I just can show a window one at the time! But in Uber App they did with both points.
This is what I did :
public void drawRoute(){
PolylineOptions po;
if(polyline == null){
po = new PolylineOptions();
for(int i = 0, tam = latLngs.size(); i < tam; i++){
po.add(latLngs.get(i));
}
po.color(Color.BLACK).width(10);
polyline = mMap.addPolyline(po);
LatLng myCurrentLocation = new LatLng(lat, lon);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myCurrentLocation, 11));
Marker mMarker;
mMarker = mMap.addMarker(new MarkerOptions().position(finalLocaltion).title(finalLocationName));
mMarker.showInfoWindow();
}
else{
polyline.setPoints(latLngs);
}
}
The window only appears when I click, and not by default!

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

Categories

Resources