Get Google Maps to automatically zoom in on my Current Location? - android

Possible duplicate of Google Maps v2 - set both my location and zoom in
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g_maps);
GoogleMap googleMap;
LatLng myPosition;
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
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();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
myPosition = new LatLng(latitude, longitude);
}
}
}
I've tried adding:
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(latitude,
longitude));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
googleMap.moveCamera(center);
googleMap.animateCamera(zoom);
or
googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude) ,4) );
but Google Maps doesn't automatically zoom in once I've hit the button which calls this method. I still get the Google UI which I can click to zoom in on my Location, but I want it to automatically zoom in.
Any help please?

Try this is simple solution for your question
LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
map.animateCamera(yourLocation);
and also..
It's possible to change location, zoom, bearing and tilt in one go. It is also possible to set the duration on the animatecamera call.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW) // Sets the center of the map to Mountain View
.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
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Have a look at the docs here:
https://developers.google.com/maps/documentation/android/views?hl=fr-FR#moving_the_camera

GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
Location locationCt;
LocationManager locationManagerCt = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationCt = locationManagerCt
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng latLng = new LatLng(locationCt.getLatitude(),
locationCt.getLongitude());
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions().position(latLng)
.title("My Spot").snippet("This is my spot!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_car)));
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}

In your XML file
<Button
android:id="#+id/Bzoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onZoom"
android:text="^" />
<Button
android:id="#+id/Bzoomout"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="onZoom"
android:text="v" />'
In your Java file
public void onZoom(View view)
{
if(view.getId() == R.id.Bzoomin)
{
mMap.animateCamera(CameraUpdateFactory.zoomIn());
}
if(view.getId() == R.id.Bzoomout)
{
mMap.animateCamera(CameraUpdateFactory.zoomOut());
}
}

For Kotlin
val location = CameraUpdateFactory.newLatLngZoom(latlng, 15F)
mMap.animateCamera(location)

Related

Trying to get User location on a map in android

My code is totally fine, i want to get user location on the map as soon as the app is opened. But the app just crashes. Also the program is set to update location and add a marker as soon as its changed, nut that's also is not working. Here's the code
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {
private GoogleMap mMap;
LocationManager locationManager;
String provider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Location location= locationManager.getLastKnownLocation(provider);
double lat =location.getLatitude();
double lng =location.getLongitude();
mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("Marker"));
// Add a marker and move the camera
}
#Override
public void onLocationChanged(Location location) {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10));
Log.i("Latitude", lat.toString());
}
try This.
getLastKnownLocation returns if location is there in cache otherwise it returns null
add a check there
if(location!=null)
{
double lat =location.getLatitude();
double lng =location.getLongitude();
mMap.addMarker(new MarkerOptions().position(new LatLng(lat,lng)).title("Marker"));
}
2 solution use google play services fusedlocation api for better management with location refer here http://coderzpassion.com/android-location-using-google-play-services/
3 try this to addMarker
public void drawMarker(double lat,double lon)
{
if (mMap != null) {
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, lon)).title(" Maps Tutorial").snippet("Android Ruler");
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
// Moving Camera to a Location with animation
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
mMap.addMarker(marker);
}
}
for more details refer here http://coderzpassion.com/android-google-maps-v2-tutorial-with-markers/

Can't add marker on User's Current Location Google Map V2

I am trying to fetch user's current location on Genymotion emulator.I already set the custom GPS longitute and latitute on Genymotion. Whenever i trying to open Google Maps the Current location can't show in it.
Here is my Code snippet.
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map1)).getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locManager = (LocationManager) context
.getSystemService(context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String locProvider = locManager.getBestProvider(criteria, false);
Location location = locManager.getLastKnownLocation(locProvider);
Location myLocation = googleMap.getMyLocation();
if (myLocation != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("rajkot")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
} else {
Toast.makeText(context, "unable to find location", 20).show();
}
This is my screenshot.
Please help me i can't find the user's current location
I also Check it on real device It's not working
Try This.
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latestlatLng = new LatLng(latitude, longitude);
Marker myself = mMap.addMarker(new MarkerOptions().position(latestlatLng).title("It's Me!"));
// myself.setDraggable(true);
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
}
});
may be you could change the getMyLocaton() method with a fusedLocationProviderClient objects getLastLocation() method.

Android: Not able to get current location in Google Map fragment

public class location_a extends Fragment {
TextView location;
MapView mMapView;
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_a, container, false);
location = (TextView) rootView.findViewById(R.id.add);
location.setText("Plot -315\nNext to Taj Hotel\nNear layout\nBangalore - 5600092\nKarnataka");
setUpMapIfNeeded();
return rootView;
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
SupportMapFragment mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment));
//Toast.makeText(getActivity(), "come in" + mMap, Toast.LENGTH_SHORT).show();
//mMap = fm.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
Toast.makeText(getActivity(), "come in", Toast.LENGTH_SHORT).show();
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(12.9120074,77.64910951)).title("Winfo Global"));
mMap.addMarker(new MarkerOptions().position(new LatLng(13.0120074,77.94910951)).title("xyz builder"));
// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
// Location myLocation = locationManager.getLastKnownLocation(provider);
Location myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (myLocation!=null) {
double longitude = myLocation.getLongitude();
double latitude = myLocation.getLatitude();
String locLat = String.valueOf(latitude) + "," + String.valueOf(longitude);
// set map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Get latitude of the current location
/// double latitude = myLocation.getLatitude();
// Get longitude of the current location
///double longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
LatLng myCoordinates = new LatLng(latitude, longitude);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(myCoordinates, 12);
mMap.animateCamera(yourLocation);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(myCoordinates) // Sets the center of the map to LatLng (refer to previous snippet)
.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
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
// mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));
}
}
HERE mMap shows always NULL so it not enter into if statment
if (mMap != null) {Toast.makeText(getActivity(), "comein", Toast.LENGTH_SHORT).show();
setUpMap();
}
pls help me to solve problem
It always null if Google Play services APK is not available in application, please can you check with your code
Try following code
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = mapFrag.getMap();
Check here for full source code and sample
Edit
Make sure you have written following code in your layout to get map fragment from id.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17);

How to add Marker in Google map

I am trying to add Google maps in my application. I get maps successfully displayed in my application.
But the Problem is I am not getting my desired position.
static final LatLng vrrittihLocation = new LatLng(23.001779, 72.618946);
private GoogleMap googleMap;
if (googleMap == null) {
googleMap = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
MarkerOptions Mo = new MarkerOptions()
.position(vrrittihLocation)
.title("Vrrittih Global Recruitment Consulting")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_marker));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(vrrittihLocation, 18);
googleMap.animateCamera(cameraUpdate);
googleMap.addMarker(Mo);
Am I missing something here?
The marker should be on ur maps, the reason u dont see your desired position maybe you can move your camera with following code.
CameraPosition cameraPosition = new CameraPosition.Builder().target(vrrittihLocation)
.zoom(18)
.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.animateCamera(cameraUpdate);
Get Latitude and Longitude through LocationManager , Like Following ,
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
and in overiden method onLocationChanged(...),
#Override
public void onLocationChanged(Location location) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
Hope this helpful to you...
googleMap.moveCamera(cameraUpdate);

How to move marker on google map V2 Android

This is my code:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
final LatLng maposition = new LatLng(latitude, longitude);
map.moveCamera(CameraUpdateFactory.newLatLng(maposition));
map.animateCamera(CameraUpdateFactory.zoomTo(16));
map.addMarker(new MarkerOptions().title("MONKEY.D").snippet("Hace la fiesta").position(maposition).icon(BitmapDescriptorFactory));}}
How can I move this marker "maposition" ?
map.addMarker(...)
returns a reference to Marker object. And that objects has a
setPosition(LatLng latlng)
method. So use it to move this marker wherever you want.
mMap.addMarker(new MarkerOptions()
.title("title")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
.position(new LatLng(latitude, longitude));

Categories

Resources