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?
Related
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
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()))
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.
I was trying few tricks using google maps api. I am trying to
Get the lat log of my current location and zoom in on the map.
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
Create a radius around my current location on the google map without the 'dot'. Just a circular radius around my location
Then use Marker to manually tap on a location on the map very close to me and get the lat long.
Basically what I am trying is I need to accurately place a marker on a building near me.
I hope I made sense. Any advise will be helpful.
Thanks
Shashi
You can try this
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LatLng myLoc = null;
myLoc = new LatLng(location.getLatitude(),
location.getLongitude());
CameraUpdate center = CameraUpdateFactory.newLatLng(myLoc); //optional
CameraUpdate zoom = CameraUpdateFactory.zoomTo(12); //optional
googleMap.addMarker(new MarkerOptions()
.position(myLoc)
.title("title you want to show")
.snippet("anything you want to show")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
How can I get the Latitude and Longitude values of a particular location that I have long clicked on the map in Android?
For the long click, I suggest you check out http://www.kind-kristiansen.no/2010/handling-longpresslongclick-in-mapactivity/. This will go into detail on how to listen for long click events within the Maps API since there is little or no built-in functionality that I know of.
As for the lat/lng code, after you get the long click you can translate the pixels to coordinates.
public void recieveLongClick(MotionEvent ev)
{
Projection p = mapView.getProjection();
GeoPoint geoPoint = p.fromPixels((int) ev.getX(), (int) ev.getY());
// You can now pull lat/lng from geoPoint
}
You'll have to manage the LongClick event, and then use the code to find out longitude and latitude with the following code:
GeoPoint geoPoint=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
int latitude = geoPoint.getLatitudeE6();
int longitude = geoPoint.getLongitudeE6();
where 'event' is the object of 'MotionEvent'.
Use any other event according to your case.
It gives latitude and longitude on which point of map click
map.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
//myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
//The code below demonstrate how to convert between LatLng and Location
//Convert LatLng to Location
Location location = new Location("Test");
location.setLatitude(point.latitude);
location.setLongitude(point.longitude);
location.setTime(new Date().getTime()); //Set time as current Date
txtinfo.setText(location.toString());
//Convert Location to LatLng
LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions()
.position(newLatLng)
.title(newLatLng.toString());
map.addMarker(markerOptions);
}
});