Iv made a location app, when I open it it opens on a map of the world and I have to hit the target icon thing in the top right hand corner for it to zoom into user location, how do I make it automatically zoom to user location when app is opened?
LocationManager locationManager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager
.getLastKnownLocation(locationManager.getBestProvider(criteria,
false));
if (location != null) {
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(d.getLatitude(), d.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets
// the
// center
// of
// the
// map
// to
// location
// user
.zoom(14) // Sets the zoom
.bearing(0) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// change marker you
}
You can use CameraPosition to show users location.
CameraPosition cameraPosition = new CameraPosition.Builder ()
.target (curentpoint).zoom (10f).tilt (70).build ();
Where target is current LatLog getting from either gps or network.
zoom will Zoom map to 10. tilt will change map's angle.
after that use animateCamera method to add cameraposition.
mGoogleMap.animateCamera (CameraUpdateFactory
.newCameraPosition (cameraPosition));
I hope this is what you want.
Related
I'm using the Google Maps API and I want to set my camera on a specific marker. When I do this and I start rotating my camera is losing focus on my marker while they both have the same coordinate.
Does someone knows why this is happening?
My code
LatLng myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
player.setPosition(myCoordinates);
CameraPosition currentPosition = map.getCameraPosition();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(myCoordinates)
.bearing(currentPosition.bearing)
.zoom(18)
.tilt(currentPosition.tilt)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Click here for a video of the problem. (The orange marker is one that the camera is set on)
Place marker on center of the screen on google map android same as like in uber and ola apps. When moving or scrolling a google map marker should not move and it should give latlng coordinates
You need to put an ImageView center of frameLayout.That is not marker of your map but it place in the center and when you click that imageview you need to get center LatLng of the map
Here is code for getting center LatLng of map :
LatLng center = mgoogleMap.getCameraPosition().target;
You need to use frame layout to align your marker in this case a image like this at center. and then fetch location using googleMap.getCameraPosition().target
for more info see http://developer.android.com/reference/com/google/android/gms/maps/model/CameraPosition.html#target
Try this,
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(currentLatitude, currentLongitude)).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Check Out This
LatLng mapCoordinate = new LatLng(yourLatitude, yourLatitude);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(mapCoordinate, 11.0f);
yourMap.animateCamera(yourLocation);
Or In Other way
LatLng mapCoordinate = new LatLng(yourLatitude, yourLatitude);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(mapCoordinate) // set to Center
.zoom(12.0f) // for the Zoom
.bearing(90) // Orientation of the camera to east
.tilt(30) // Tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition
yourMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
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));
I am using this code for load google map
private GoogleMap myMap;
if(myMap!=null){
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
marker = myMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(getAddress(latitude, longitude)));
Location = new LatLng(latitude, longitude);
CameraPosition cameraPosition = new CameraPosition.Builder().target(Location).zoom(15)
.bearing(90) // Sets the orientation of the camera to
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
// myMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 15));
}
but map is not loading and occurring this issue
You must install the latest Google play services in your phone/tab/emulator before running the app.
For google play on emulator.
I'm trying to start google maps in my position, but I can not, but I can put a marker at the position you want it to be Initial. I need your help please. I searched a lot here, but nothing has worked for me.
Best Regards
Code
#TargetApi(Build.VERSION_CODES.GINGERBREAD) #SuppressLint("NewApi") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
getSreenDimanstions();
fragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
map = fragment.getMap();
bNavigation = (Button) findViewById(R.id.bNavigation);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
miPosicion = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().position(miPosicion).title("Start"));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
obtenerItems(map);
}
I used this documentation which uses:
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 17.0f);
theMap.animateCamera(update);
If you want to get the exact current location, you will be better off calling
requestLocationUpdates(..)
rather than
getLastKnownLocation(..)
and then implementing a LocationListener to listen to changes in location and act accordingly. For more information regarding LocationListener refer:
http://developer.android.com/reference/android/location/LocationListener.html
And on a side note: you seem to have initialized two LatLng types(latLng and miPosicion) with the exact same value and have not put either to full use.