moveCamera with CameraUpdateFactory.newLatLngBounds crashes
OK I worked this out. As documented here that API can't be used pre-layout.
The correct API to use is described as:
Note: Only use the simpler method newLatLngBounds(boundary, padding)
to generate a CameraUpdate if it is going to be used to move the
camera after the map has undergone layout. During layout, the API
calculates the display boundaries of the map which are needed to
correctly project the bounding box. In comparison, you can use the
CameraUpdate returned by the more complex method
newLatLngBounds(boundary, width, height, padding) at any time, even
before the map has undergone layout, because the API calculates the
display boundaries from the arguments that you pass.
To fix the problem I calculated my screen size and provided the width and height to
public static CameraUpdate newLatLngBounds(
LatLngBounds bounds, int width, int height, int padding)
This then allowed me to specify the bounding box pre-layout.
Google Map-Android Centering and Zooming?
In order to use just one location from an address, and set the zoom on that one marker, you can use CameraPosition
instead of LatLngBounds
.
I just tested this, and it works:
private void setUpMap(String address) throws IOException {
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(address, 1);
Address add = list.get(0);
double Longitude = add.getLongitude();
double Latitude= add.getLatitude();
Marker m = mMap.addMarker(new MarkerOptions().position(new LatLng(Latitude, Longitude)).title(address));
//not needed:
//LatLngBounds.Builder builder = new LatLngBounds.Builder();
//builder.include(m.getPosition());
//LatLngBounds bounds = builder.build();
//mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));
//use CameraPosition instead:
LatLng latLng = new LatLng(Latitude, Longitude);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng).zoom(14).build();
//use animateCamera instead of moveCamera:
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
Calling the method:
try {
setUpMap("100 Market Street San Francisco CA");
} catch (IOException e) {
e.printStackTrace();
}
Result:
Note that usually when using LatLngBounds
, you would initially provide two points that are the Northeast and Southwest corners of the bounds.
Once those two bounds have been set, you can use the include()
method to expand the bounds if necessary.
See Documentation:
https://developer.android.com/reference/com/google/android/gms/maps/model/LatLngBounds.html
https://developers.google.com/maps/documentation/android/views
Why does GoogleMap.animateCamera() crash after onGlobalLayout()?
I am not sure but as you are using AsyncTask is probably that you are trying to get map bounds before map has undergone layout.
You can see the documentation, it suggest to use newLatLngBounds(boundary, width, height, padding)
instead .
Add bounds to map to avoid swiping outside a certain region
You can use MapLoadedCallBack;
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 30));
}
});
and also you can use this event that occurs prier to above.
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 30));
}
});
Android Google Maps moveCamera causes IllegalStateException: Illegal height
Even though the crash happened on moveCamera
, the problem seemed to be caused by setPadding
called above it:
googleMap.setPadding(
0,
topPadding,
0,
0,
)
The fix was making sure that the padding would not exceed half of the map size:
googleMap.setPadding(
0,
min(topPadding, map.height / 2 - 2),
0,
0,
)
getting Application crash when retrieving values from one activity to another
May be you are trying to call direct map Activity and lat and lon are not getting in your activity so that its null and your activity crash.First call Plugin and pass that parameter to activity
Related Topics
Removing Unused Strings During Proguard Optimisation
How to Open an Android Studio Project in Eclipse
Error Strictmode$Androidblockguardpolicy.Onnetwork
How to Force the Action Bar to Be at the Bottom in Ics
Visual Studio (2015) Emulator for Android Not Working - Xde.Exe - Exit Code 3
How to Get Access Token After User Is Signed in from Gmail in Android
Android:Static Fields and Memory Leaks
Error: Status{Statuscode=Developer_Error, Resolution=Null}
How to Open a PDF via Intent from Sd Card
How to Set Time to Device Programmatically
How to Resolve Target 'Android-Xx'
Android: How to Select Texts from Webview
How to Scale/Resize Text to Fit a Textview
How to Solve the Install_Failed_Dexopt Error
Android M Camera Intent + Permission Bug
How to Add Buttons at Top of Map Fragment API V2 Layout
Android App Crashes When Launched in Debug Mode
Safely Fixing: Javax.Net.Ssl.Sslpeerunverifiedexception: No Peer Certificate