I can't start the map on my location - android

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.

Related

How to Zoom and get latitude and longitude from mMap.setMyLocationEnabled(true) in Google Map (Android)

I was setting mMap.setMyLocationEnabled(true) and I am successfully getting my current location but...
Not able to zoom to that position.
Not able to get lat and lng from that position
From getting latitude and longitude I am using below code but it gives me null. I am calling below method from onMapReady(Google Map) and passing google map object.
private void goToCurrentLocation(GoogleMap map) {
Log.d("Tag", "inside");
Location loc = map.getMyLocation();
if (loc != null) {
LatLng point = new LatLng(loc.getLatitude(), loc.getLongitude());
Log.d("Tag=", "latlng=" + point);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point, 15));
}
}
Is there any alternative to getting latitude and longitude as getMyLocation deprecated?
use link provided in the answer to get current location and then zoom over that location using below code:
latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
mGoogleMap.animateCamera(cameraUpdate);
http://blog.teamtreehouse.com/beginners-guide-location-android

Location query in Google maps in android

I have made an app utilizing Google Maps. I have added a marker of my current location in it. However, as soon as I open the app, I want it to zoom in to my location instead of showing the whole world at the 95302 23535919711008489 location first. How do I accomplish this?
Use animateCamera() as below
#Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
map.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);
}
Please read the docs
LatLng cameraLatLng = new LatLng(savedLat, savedLng);
googleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
googleMap.animateCamera(CameraUpdateFactory
.newLatLngZoom(cameraLatLng, 7))
savedLat, savedLng your current location longitude and latitude.You can change zoom number ( 7 ) to your fitting.

Android studio location

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.

How to put marker on android map location?

After I find this Location mCurrentLocation = mLocationClient.getLastLocation(); how can I add a marker to that that location?
Making a marker like
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude))
requires a position object, is there a way that I can retrieve a position from the Location object? or is this done in some other way?
You can use the getLatitude() and getLongitude() of the Location class which will return double which by then you can pass it in your new LatLong constructor.
sample:
MarkerOptions marker = new MarkerOptions().position(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()))

Android Update Current Location

So here is my method for getting the user saved location and then move camera to that location:
private void updatePlaces(){
locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double lat = lastLoc.getLatitude();
double lng = lastLoc.getLongitude();
LatLng lastLatLng = new LatLng(lat, lng);
if(userMarker!=null) userMarker.remove();
userMarker = theMap.addMarker(new MarkerOptions()
.position(lastLatLng)
.title("You are here")
.icon(BitmapDescriptorFactory.fromResource(userIcon))
.snippet("Your last recorded location"));
theMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 3000, null);
}
How can i modify this code in order to get the new position only once, compare and then get camera to it?
Thank you in advance.
I don't understand you question, if you run the method updatePlaces() only ones it will getLastKnownLocation only once and updates the user marker only once. could you please be more explanatory on what you are trying to achieve?

Categories

Resources