How to draw a path between two locations in google maps - android

I'm developing an android app in which I need to show the Google Map with the path drawn from source to destination.For Example I need to go from A to B there might be different routes but I need to draw only on a particular route.
This is what tried but it was not taking on any road it directly displaying a line from source to destination.
public class MapsActivity extends Activity {
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
}
/* googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
Marker TP = googleMap.addMarker(new MarkerOptions().
position(TutorialsPoint).title("My Location"));*/
Polyline line = googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(17.4489224, 78.3483612), new LatLng(17.4831854, 77.9736794))
.width(5)
.color(Color.RED));
}
catch (Exception e) {
e.printStackTrace();
}
}

First you have to create a Polyline object to specify the points, then you can set the Polyline object adding the Polyline options:
PolylineOptions polylineOptions = new PolylineOptions()
.add(new LatLng(37.35, -122.0)); // Point A.
.add(new LatLng(38.35, -123.0)); // Point B.
Polyline polyline = mMap.addPolyline(polylineOptions);
To alter the shape of the polyline after it has been added, you can call Polyline.setPoints() and provide a new list of points for the polyline.

Related

Dynamically draw a single polyline to current location

In my android app, I want to create a single path/polyline from a specific location(far end) to my current location while moving. I could draw the polyline on map with now issue while when I moves to a new location another polyline is been drown on the map, so my map output is full of polylines whichh I don't need, I want only one polyline to be visible to my current location.
PolylineOptions polylineOptions = new PolylineOptions().add(currentPosition).add(farEndPosition).width(10).color(Color.GREEN);
Polyline polyline = googleMap.addPolyline(polylineOptions);
Remove old Line before adding new one.
Polyline polyline ;
public void addUpdatePolyLine()
{
PolylineOptions polylineOptions = new PolylineOptions().add(currentPosition).add(farEndPosition).width(10).color(Color.GREEN);
if(polyline !=null)
{
polyline.remove();
}
polyline = googleMap.addPolyline(polylineOptions);
}

GoogleMaps draw multiple polylines even i am removing the previous polyline before plotting new one

I am using google maps and drawing polylines on it by passing array of coordinates on each location update. After plotting the polyline i first remove the previous one and then i am plotting new polyline based on updated coordinates array list. Problem is, google maps is plotting a separate polyline from starting to end point on each location update.
I know mistake is somewhere in the hierarachy of calling things. Can anybody help me figuring this out.
This is the method in which i am updating the camera focus and plotting polylines.
private void moveCamera(Location location) {
if (location != null) {
if (polyline != null) {
polyline.remove();
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
coordList.add(new LatLng(location.getLatitude(), location.getLongitude()));
// 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
polyline = mMap.addPolyline(polylineOptions);
}
}
just call
polylineOptions.clear(); -> delete current object
and
map.clear() -> invalidate map
before add new polyline
Got the Solution. I was using the same old PolylineOptions again and again by declaring it as global variable. Now before each plot i create a new instance of PolylineOptions and now good to go.
Here is the updated code.
private void moveCamera(Location location) {
if (location != null) {
if (polyline != null) {
polyline.remove();
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
coordList.add(new LatLng(location.getLatitude(), location.getLongitude()));
polyline = mMap.addPolyline(new PolylineOptions().addAll(coordList)
.width(5)
.color(Color.RED));
}
}

(Android) Is it possible to change the shape of the polyline in google maps?

I know we can change the color of the polyline by using "polylineOptions.color(YOUR_COLOR);" bit is it possible to set a shape on it? (I would like the like to have a blue border and be white inside if the route that needs to be completed and if completed, set the color to blue -no more border)
This is what I have so far, I call this function 2 times, once for the completed path and then for the path to be completed:
public void createProgressRouteOnMap( ArrayList<LatLng> route, boolean done){
if(done) {
if(polyline != null){
polyline.remove();
}
}else{
if(polyline2 != null){
polyline2.remove();
}
}
PolylineOptions polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(route);
polyLineOptions.width(10);
if(done){
polyLineOptions.color(getResources().getColor(R.color.background));
}else{
polyLineOptions.color(getResources().getColor(R.color.blue_route));
}
if(mMap == null){
setUpMapIfNeeded();
}
if(done) {
polyline = mMap.addPolyline(polyLineOptions);
}else{
polyline2 = mMap.addPolyline(polyLineOptions);
}
}
This will do?
GoogleMap map;
// Add a thick blue line
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng1, new LatLng2)
.width(10)
.color(Color.BLUE));
// Add a thin white line
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng1, new LatLng2)
.width(4)
.color(Color.WHITE));

Android map v2 draw custom rectangle

I'm new to the google maps v2, I need to draw a rectangle from point1 to point2, with a custom color and width, I tried to use a Polyline, but all the labels (city name, country name ..) are still visible, so how can I do this ?
thank you
this is what I tried
mMapView.addPolyline(new PolylineOptions()
.add(new LatLng(xx,xx), new LatLng(xx,xx))
.width(50)
.color(Color.parseColor("#f4f3f0")));
Just read the Documentation
Example
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));
Use
public class My_Map extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(-18.142, 178.431), 2));
// Polylines are useful for marking paths and routes on the map.
map.addPolyline(new PolylineOptions().geodesic(true)
.add(new LatLng(-33.866, 151.195))
.add(new LatLng(-18.142, 178.431))
.add(new LatLng(21.291, -157.821))
.add(new LatLng(37.423, -122.091))
);
}
}
For more details just check How to draw free hand polygon in Google map V2 in Android?

How can I get the visible markers in Google Maps v2 in Android?

In Google Maps v2 for Android, how can I get the visible markers? I know I can use Projection and eliminate points < 0 and points > screen size. But I do not wish to check one by one, it can be too slow if I have a lot of markers. Is there any easy way? Or some off-the-shelf solution? If yes, which one?
Ok, the following is the code the I have used before to determine what the user can see and then only draw the markers that are visible.
I think you might be able to adapt it to your purpose.
get the current rectangle "viewport" of the map (note:must be run on the main thread)
this.mLatLngBounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;
sort the 2 points (top-left and bottom-right) so that we can use min/max logic
double lowLat;
double lowLng;
double highLat;
double highLng;
if (this.mLatLngBounds.northeast.latitude < this.mLatLngBounds.southwest.latitude)
{
lowLat = this.mLatLngBounds.northeast.latitude;
highLat = this.mLatLngBounds.southwest.latitude;
}
else
{
highLat = this.mLatLngBounds.northeast.latitude;
lowLat = this.mLatLngBounds.southwest.latitude;
}
if (this.mLatLngBounds.northeast.longitude < this.mLatLngBounds.southwest.longitude)
{
lowLng = this.mLatLngBounds.northeast.longitude;
highLng = this.mLatLngBounds.southwest.longitude;
}
else
{
highLng = this.mLatLngBounds.northeast.longitude;
lowLng = this.mLatLngBounds.southwest.longitude;
}
then in my case I had this data in a db, so i could use >= and <= to extract only the pins i wanted
You could use the android-map-extension library. Amongst others it offers a List GoogleMap.getDisplayedMarkers() method.
You could add this Extension Function to GoogleMap class if you are using Kotlin
fun GoogleMap.isMarkerVisible(markerPosition: LatLng) =
projection.visibleRegion.latLngBounds.contains(markerPosition)
You just pass the marker position as a parameter of this method and do whatever you want with the result.
If you are using Java you can just declare the function wherever fits better for you.
Hope it helps!
You are looking for either markers:
https://developers.google.com/maps/documentation/android/marker
private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Hello world"));
Or direct drawing:
https://developers.google.com/maps/documentation/android/shapes
// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
.add(new LatLng(37.35, -122.0))
.add(new LatLng(37.45, -122.0)) // North of the previous point, but at the same longitude
.add(new LatLng(37.45, -122.2)) // Same latitude, and 30km to the west
.add(new LatLng(37.35, -122.2)) // Same longitude, and 16km to the south
.add(new LatLng(37.35, -122.0)); // Closes the polyline.
// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);

Categories

Resources