I am using the google maps api v2 for my new google maps app. And I wondered how to calculate the coordinates of 4 edges of the current screen position so that I can search for markers that are directly in the camera view. Any ideas about how it is possible to achieve ? I thought about something with the center of the camera view and zoom but can't figure out how to continue.
Here is the proper answer to get the coordinates of upper right corner and down left corner:
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
//fetchData(bounds);
ne = bounds.northeast;
sw = bounds.southwest;
//new DownloadJSON().execute();
//Log.e("LatLngs"," southwest: "+sw+" northeast: "+ne);
}
});
Related
Is there any way to prevent user scroll pass a specific area using Google map?
I added a ground overlay on the map, and I don't want user scroll pass it. I found https://developers.google.com/maps/documentation/android-api/views#restrict-panning but user still can see a little map around the overlay (see image below). Any suggestions will be appreciated.
My code:
#Override
public void onMapReady(final GoogleMap mMap) {
final LatLngBounds bounds = new LatLngBounds(new LatLng(10.762657076386494, 106.65704530350968), new LatLng(10.783062032257044, 106.67223733537003));
mMap.addGroundOverlay(new GroundOverlayOptions().image(BitmapDescriptorFactory.fromResource(R.drawable.images))
.positionFromBounds(bounds));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(bounds.getCenter(), 18));
mMap.setLatLngBoundsForCameraTarget(bounds);
}
I am using Mapbox android sdk for my map application, i want to use pointer icon('myIcon' in this case) which always points in the direction where user is moving.
I here is my code :
public void addMarker(MapboxMap mapboxmap) {
// marker view options : setting location and icon
MarkerViewOptions options = new MarkerViewOptions()
.position(latLng)
.anchor(0.5f, 0.5f)
.icon(myIcon);
MarkerView view = options.getMarker();
// added marker on map
mapboxMap.addMarker(options);
}
With marker views we don't expose a way to do this, and your only option is to adjust the marker rotation when the camera is rotated. A better solution would be to use runtime styling and a symbol layer. An example of this can be found in our demo app. To ensure that the marker always points in the correct direction you can use the icon-rotation-alignment property and set it to map. Hope this helps!
Setting rotation on location update {like setRotation(angle_you_want_to_set)} worked of me.
Example :
public void addMarker(MapboxMap mapboxmap, float angle) {
// marker view options : setting location and icon
MarkerViewOptions options = new MarkerViewOptions()
.position(latLng)
.anchor(0.5f, 0.5f)
//set angle of ration for icon
.setRotation(angle)
.icon(myIcon);
MarkerView view = options.getMarker();
// added marker on map
mapboxMap.addMarker(options);
}
Android Google maps V2 How to draw a Custom Views(1/3 filled circle) on map where can i get canvas object to draw.As all new classes are final classes I can't extend them.
I need to draw a some cutom views on the map.I am upgrading from v1 to v2.The Overlays has been deprecated.I tries with Tile Provider but it is not drawing anything.
exampleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GroundOverlayOptions overlayOptions = new GroundOverlayOptions()
.image(BitmapDescriptorFactory.fromResource(R.drawable.testImage))
.position(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 8600f, 6500f);
map.addGroundOverlay(overlayOptions);
}
});
This is the code to draw an image at a specified lat long which is working for google maps v2. You can basically get an image of 1/3 filled circle and set it to the lat long you want.
Hope it helps.
I have a google map (com.google.android.gms.maps.GoogleMap) where I have some markers set.
I am able to, separately,
1) adjust zoom level and center the map on a boundary:
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10));
and
2) center the map above one of the markers:
LatLng poiSelectedLatLng = new LatLng(markerSelected.getPosition().latitude
+ offset, markerSelected.getPosition().longitude);
mMap.animateCamera(CameraUpdateFactory.newLatLng(poiSelectedLatLng));
but, for the life of me, I can't just do both, adjust the zoom level using newLatLngBounds and then center the map somewhere else. Whatever I do last is what I see happening in the map.
How do I do this?
For future visitors this is how you can chain camera animations:
map.animateCamera(CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10), 2000, new CancelableCallback() {
#Override
public void onFinish() {
LatLng poiSelectedLatLng = new LatLng(markerSelected.getPosition().latitude + offset, markerSelected.getPosition().longitude);
map.animateCamera(CameraUpdateFactory.newLatLng(poiSelectedLatLng));
}
#Override
public void onCancel() {
}
});
Also see AnimateCameraChainingExampleActivity.java for an example how to chain infinitely.
Try using both moveCamera and animateCamera...
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10));
LatLng poiSelectedLatLng = new LatLng(markerSelected.getPosition().latitude
+ offset, markerSelected.getPosition().longitude);
mMap.animateCamera(CameraUpdateFactory.newLatLng(poiSelectedLatLng));
moveCamera will move directly to that spot while animateCamera will provide the moving effect. They are linear in nature so one will happen after the other however layering them as I have done above will provide the potential effect you are looking for.
If you are trying to see the actual movement of both calls on the UI you will need to register for the callback post the completion of the animation as needed.
I am using Google maps api v2 in android. I have placed a marker by using latitude and longitude . The marker is shown at correct place , but i want the the map should show area around the marker only .i.e i want to zoom to markers position when the map is shown so it shows nearby region of the marker only..any help would be great.
pass your current location in this function where you have placed your marker.
private void moveToCurrentLocation(LatLng currentLocation)
{
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,15));
// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomIn());
// Zoom out to zoom level 10, animating with a duration of 2 seconds.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}
Provide marker position.
private void pointToPosition(LatLng position) {
//Build camera position
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(position)
.zoom(17).build();
//Zoom in and animate the camera.
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}