In my android app, I have added google map and I load dynamically more points using specific lat / long values and specific pins
This is my code:
GoogleMap map = holder.mapView.getMap();
if(map != null)
{
try {
MapsInitializer.initialize(activity);
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
map.clear();
map.getUiSettings().setZoomControlsEnabled(true);
LatLng latLng=new LatLng(gS.getLat(), gS.getLon());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
CameraUpdate center=
CameraUpdateFactory.newLatLng(latLng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(10);
//map.animateCamera(cameraUpdate);
map.moveCamera(cameraUpdate);
map.animateCamera(zoom);
if(time_diff > day3 && magnitude >= 0 && magnitude < 2)
map.addMarker(new MarkerOptions()
.draggable(false)
.anchor(0, 1)
.position(new LatLng(gS.getLat(), gS.getLon()))
.title("").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_lt2m)));
The code works fine and loads the data into map with custom pin, but I have seen that when I zoom out or zoom in the map, the marker moves its position instead of remaining in its actual lat long position.
Any help?
Thanks
I use API V2. it's possible that a google play service old version create this?
Related
I cannot find the way to move android google map camera with constant velocity and zoom.
For example, if the current zoom is 15, and calling
CameraPosition pos= CameraPosition
.builder(map.mMap.getCameraPosition())
.target(target)
.zoom(15)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(pos), duration * 1000, null);
it modifies the zoom 15 - > 12 -> 15
(the mid value depends on the distance)
My target is to show an object moving on the map with constant velocity and always keep it in the center of the map
Tried to call moveCamera in the ValueAnimator of the object, but then the map jumps. I need to move it smoothly
Use fused API for location update and move the camera to the updated location
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(MainActivity.this, new OnSuccessListener<android.location.Location>() {
#Override
public void onSuccess(android.location.Location location) {
if(location!=null){
try{
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
String addresses = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 1).get(0).getAddressLine(0);
marker_origin = new MarkerOptions().
position(latLng).title(addresses)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mMap.addMarker(marker_origin);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16.0f));
getPlacesData(latLng, "restaurant");
}catch (Exception e){
e.printStackTrace();
}
}
}
});
For further details check the android developer documentation
https://developer.android.com/training/location/receive-location-updates.html
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
I am trying to get the android map to zoom on my marker & leave the marker in the center of the screen.
public void onNewLocation(Location location, String name) {
if (marker == null){
marker = new MarkerOptions().position(new LatLng(
location.getLatitude(),
location.getLongitude()))
.title(preferencesUtils.getName(getApplicationContext(), preName, key));
}
map.clear();
marker.position(new LatLng(location.getLatitude(), location.getLongitude()));
map.addMarker(marker);
}
this is the code - what should i add to zoom my marker
To simply zoom in, use:
float zoomLevel = 20.0f;
map.animateCamera(CameraUpdateFactory.zoomTo(zoomLevel);
To zoom to the marker, use:
LatLng l = new LatLng(LATPOSITION, LONGPOSITION);
float zoomLevel = 20.0f;
map.animateCamera(CameraUpdateFactory.newLatLngZoom(l, zoomLevel));
Add these where you want the zoom to happen.
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());
Currently migrating an Android app using Google Maps to Google Maps Android v2; I want the map to display the built-in location marker, as well as custom markers.
With the previous library, I used to draw the location marker 'below' custom markers, with
mapView.setReticleDrawMode(ReticleDrawMode.DRAW_RETICLE_UNDER);
I can't find a similar option in the new API. Did I miss something?
After a bit a searching, I couldn't find such an option.
I finally removed the location pin, and added a similar Marker and Circle to the map; since it is a classic Marker, it appears below the other markers.
map.setMyLocationEnabled(false);
// ...
// get the current location with Android LocationManager in some way
// ...
// then draw it on the map when it changes:
if (null == getMyLocation())
return;
if (null != locationMarker) {
locationMarker.remove();
}
if (null != locationCircle) {
locationCircle.remove();
}
LatLng loc = getMyLocation();
locationMarker = map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.blue_pin_12))
.position(getMyLocation()).anchor(0.5f, 0.5f));
float accuracy = getMyLocationAccuracy();
locationCircle = map.addCircle(new CircleOptions().center(loc)
.radius(accuracy)
.strokeColor(Color.argb(255, 0, 153, 255))
.fillColor(Color.argb(30, 0, 153, 255)).strokeWidth(2));