Location query in Google maps in android - 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.

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

Android Google Maps - remove location accuracy circle

I'm displaying a Google map (v2 API) in an Android app like so:
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
Requesting location:
mLocationClient = new LocationClient(getApplicationContext(), this, this);
mLocationClient.connect();
What I want to do is show just the marker for the current location, and not the circle indicating location accuracy. I've looked through the documentation and API reference, and searched around and haven't found an option to turn that off.
The blue circle on the map is enabled eith the following line:
map.setMyLocationEnabled(true);
You only have to set it to false and it will disappear:
map.setMyLocationEnabled(false);
To draw your marker you need to get the users location using a listener. You can do something like this:
GoogleMap.OnMyLocationChangeListener locationListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
drawMarker(location);
private void drawMarker(Location location) {
LatLng currentPosition = new LatLng(location.getLatitude(),
location.getLongitude());
map.addMarker(new MarkerOptions()
.position(currentPosition)
.snippet(
"Lat:" + location.getLatitude() + "Lng:"
+ location.getLongitude())
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("position"));
}
};
map.setOnMyLocationChangeListener(locationListener);
You can also use your LocationClient and do the same if you need to.
I got rid of that annoying circle like this:
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));

I can't start the map on my location

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.

google maps api circular radius and marker together

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 to get latitude and longitude of map in using Android google mapV2

I am looking for latitude and longitude values of top right corner of mapView using Android google maps V2. I posted similar question on the same issue, but no use.
I wrote code for this,but getting zero values.
please check my code:
MapView mapView = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLngBounds bounds = mapView.getProjection().getVisibleRegion().latLngBounds;
LatLng topright = bounds.northeast;
double toprgt_latitude = topright.latitude;
Log.d("top lat", "" + toprgt_latitude);//getting zero values.
Use mapView.getProjection().getVisibleRegion().latLngBounds.northeast after map is shown.
LatLng corner;
mapView.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
corner = mapView.getProjection().getVisibleRegion().latLngBounds.northeast;
}
}
LatLng currentposition = new LatLng(location.getLatitude(), location.getLongitude());
sys.out.println(""+location.getLatitude()""+location.getLongitude());

Categories

Resources