Show route direction in Google Maps - android

googleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
googleMap.addMarker(new MarkerOptions()
.position(
new LatLng(Float.parseFloat(latitude), Float
.parseFloat(longitude)))
.title("Marker").title(title));
**googleMap.addPolyline(new PolylineOptions()
.add(new LatLng( Double.parseDouble(latitude1), Double.parseDouble(longitude1)),
new LatLng( Double.parseDouble(latitude), Double.parseDouble(longitude)))
.width(5).color(Color.BLUE).geodesic(true));;**
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
cameraLatLng, 17));
So basically that's what I have done, but what that code does is it draws a line from a source to a destination. However, I do not want to draw a line, I just want to show route direction.
like this picture http://s7.postimg.org/dwdbkq6h7/Screenshot_2015_04_14_20_40_58.png

You can use the Google Direction API to find a route between two points.

Related

Google Marker is moving while using achor in MarkerOption

I was trying to place a marker in Google Map using the following code.
googleMap.clear();
//Dont know why this is 4.8f...
googleMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.google_marker))
.position(ll).flat(true)
.anchor(0.5f, 0.5f));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(ll)
.zoom(initMapZoom) // Sets the zoom
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
cameraPosition = new CameraPosition.Builder()
.target(ll)
.zoom(finalMapZoom) // Sets the zoom
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Now, If I zoom in / out the marker moves from the actual place. If I just remove this
.anchor(0.5f, 0.5f));
Its working fine. The problem is I would like to put the marker in the upper/top position of the screen. So I used .anchor(0.5f, 4.8f)); which is also wrong according to the documentation . The value should be less or equal to 1.
Any ideas to resolve this issue??
Thanks in Advance.

How to clear Google map v2 except few markers?

Suppose i have 100 markers on the map what i want is when i apply GoogleMap.clear(); to clear the map it clear all the other markers on the map except 2 markers and 1 polyline between them a path.
say
marker1 = GoogleMap .addMarker(new MarkerOptions().position(latLng1).title(A));
marker2 = GoogleMap .addMarker(new MarkerOptions().position(latLng2).title(B));
line = GoogleMap.addPolyline(options1);
I don't want to clear these three. I want this so user don't have to experience a blink.
There is no way to clear everything except some things. However, you can keep a reference to any markers that you want to clear and loop over them.
ArrayList<Marker> markersToClear = new ArrayList<Marker>();
marker1 = GoogleMap.addMarker(new MarkerOptions().position(latLng1).title(A));
marker2 = GoogleMap.addMarker(new MarkerOptions().position(latLng2).title(B));
marker3 = GoogleMap.addMarker(new MarkerOptions().position(latLng3).title(C));
markersToClear.add(marker2);
markersToClear.add(marker3);
for (Marker marker : markersToClear) {
marker.remove();
}
markersToClear.clear();
// marker1 left on map

Create Marker but not show it on the map

I would like to know how I can create a Marker but not to show on the map, I'm saving the Markers in an array but it is showing me on the map, this is my code
if (c4!= null ) {
if (c4.moveToFirst()) {
do {
LatLng posicion = new LatLng(
Double.parseDouble(c4.getString(0)),
Double.parseDouble(c4.getString(1)));
punto = googleMap.addMarker(new MarkerOptions() .position(posicion)
.title(c4.getString(2))
.snippet(year)
.anchor(0.5f, 0.5f));
mMarkers.add(punto);
}while (c4.moveToNext());
}
}
I've tried and used this
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude));
but this does not work for me as I need to be type Marker.
Anyone know how could i save spots without paint on the map? thank you very much and sorry for my bad English :)
Try using:
punto = googleMap.addMarker(new MarkerOptions() .position(posicion)
.title(c4.getString(2))
.snippet(year)
.anchor(0.5f, 0.5f).visible(false));
instead of
punto = googleMap.addMarker(new MarkerOptions() .position(posicion)
.title(c4.getString(2))
.snippet(year)
.anchor(0.5f, 0.5f));
Notice the .visible(false) part.
For further info regarding the same refer: http://developer.android.com/reference/com/google/android/gms/maps/model/MarkerOptions.html
save the markers in this list
List<Marker> markers = new ArrayList<Marker>();
markers.add(marker); // here the marker is of google map
Then afterwords use Clear() on your google map so that the markers that got add should be deleted
EDIT=
Also you can separately remove a single marker by
marker.remove();
you can also try to save the latitude and longitude in array list and then later as you like it, show them through marker.

how to zoom and add marker to android mapfragment

I am displaying a map at my android application (google maps api v2).
I want to manipulate the map in order to show a specific location, zoom and marker.
can anyone give an example of manipulation mapfragment
To get map instance in code do this:
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
or:
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
depending on whether you used SupportMapFragment or MapFragment in your XML file.
Then to add a marker to it:
Marker newmarker = map.addMarker(new MarkerOptions().position(latlng).title("marker title").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_purpul)));
To zoom the map:
CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(14.0f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
map.moveCamera(cameraUpdate);

Unwanted polyline extension when zoom in and disappears on zoom out on android Map

I connected map locations using Polyline API. It creates polyline properly.But sometimes unwanted polyline extended when zoom in and disappears on zoom out.
My code:
locations.clear();
for(int i=0;i<maplatitude.size();i++)
{
locations.add(new LatLng(maplatitude.get(i), maplongitude.get(i)));
}
PolylineOptions rectOptions = new PolylineOptions();
rectOptions.addAll(locations);
// Get back the mutable Polyline
Polyline polyline = mMap.addPolyline(rectOptions);
for(int i=0;i<maplatitude.size();i++)
{
//mMap.addMarker(new MarkerOptions().position(new LatLng(maplatitude.get(i), maplongitude.get(i))).title(name).snippet(mapdate.get(i)+"\n"+maptime.get(i)));
mMap.addMarker(new MarkerOptions().position(new LatLng(maplatitude.get(i), maplongitude.get(i))).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)).title(String.valueOf(i)));
}
I solved this: it's a bug from Google. We must filter some close points within 1 or 2 meters.
Polylines appearing on map where they shouldn't

Categories

Resources