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);
Related
how can i set the zoom level of a SupportMapFragment ?
I tried some answers I found in similar questions, like this one answered buy a guy named Rob who proposed:
LatLng currentLocation = mService.getCurrentLocation();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(currentLocation) // Sets the center of the map to the current location of the user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
But this solution didn't work. So, how can I set the Zoom level knowing that I use a SupportMapFragment.
if your current location is not null then try this code it will work
LatLng currentLocation = mService.getCurrentLocation();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentLocation , 16);
mMap.addMarker(new MarkerOptions().position(location).title(""));
mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation ));
mMap.animateCamera(cameraUpdate)
Marker currentMarker;
MarkerOptions marker = new MarkerOptions().position(new
LatLng(Double.
parseDouble(lat),Double.parseDouble(lon))).title(title);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_schoolbus));
currentMarker = map.addMarker(marker);
currentMarker.showInfoWindow();
currentMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_schoolbus));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(lat),Double.parseDouble(lon)), 15));
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.
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
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(location.getLatitude(),location.getLongitude()))
.title("Me")
.snippet("Population: 776733"));
When I run the code,google map is supposed to show the place where I am with the marker,but the map is opening up with some other place,no my current location,and I have to move to my location manually to find the marker.
You should animateCamera on that particular location Marker like
LatLng current_loc= new LatLng(lat, lng);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(current_loc, 18.0f));
For more information go to https://developers.google.com/maps/documentation/android/views#moving_the_camera
try this,
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(your latitude, you longitude)));
I have got the following code, and what I want to do is to open "Google Maps" APP [How to go] to the marker I have set up:
// S E T M A P P O I N T
mLocationClient = new LocationClient(this, this, this);
mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
map = mapFragment.getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setZoomControlsEnabled(true);
map.getUiSettings().setCompassEnabled(true);
map.setOnMapClickListener(this);
map.setMyLocationEnabled(true);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), ZOOM));
map.clear();
addressText = GetGeolocation.getgeocoder(Date_Accept.this, latitude, longitude);
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(addressText)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
marker.showInfoWindow();
How can I open "Google Maps" app to get the route to that point?
you should be able to fire an intent with the maps url and let android handle it for you
read about that here