I've faced strange behaviour of Google Map v2 on my Nexus7 when trying to place camera in specified position.
Code:
public class PlacesFragment extends MapFragment {
GoogleMap mapView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mapView = getMap();
mapView.moveCamera(
CameraUpdateFactory.newLatLng(new LatLng(50.4293817, 30.5316606)));
mapView.moveCamera(CameraUpdateFactory.zoomTo(11));
}
This piece of code moves camera to specified position on Nexus4, but on Nexus7 2013 it moves camera to (19.1599396,30.5316606) position, which has right longitude, but not latitude.
I've found a workaround, but I'm still interested why this happens.
For those who have this problem this is the solution:
mapView.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(50.4293817, 30.5316606), 11));
Related
Is there any way to prevent user scroll pass a specific area using Google map?
I added a ground overlay on the map, and I don't want user scroll pass it. I found https://developers.google.com/maps/documentation/android-api/views#restrict-panning but user still can see a little map around the overlay (see image below). Any suggestions will be appreciated.
My code:
#Override
public void onMapReady(final GoogleMap mMap) {
final LatLngBounds bounds = new LatLngBounds(new LatLng(10.762657076386494, 106.65704530350968), new LatLng(10.783062032257044, 106.67223733537003));
mMap.addGroundOverlay(new GroundOverlayOptions().image(BitmapDescriptorFactory.fromResource(R.drawable.images))
.positionFromBounds(bounds));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(bounds.getCenter(), 18));
mMap.setLatLngBoundsForCameraTarget(bounds);
}
In my app I use GoogleMap (play-services-maps:10.2.1). I've fixed the position of the map on a specific location and I don't want my user to be able move the map. I only want him to be able to zoom on it.
Here's what I tried :
// Set position
LatLng requestedPosition = new LatLng(lat, lon);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
// Disable all Ui interaction except for zoom
map.getUiSettings().setAllGesturesEnabled(false);
map.getUiSettings().setZoomGesturesEnabled(true);
It looks like it work at first sight but in fact while zooming and dezooming the camera position change a little on every zoom movement.
I have no idea what to do.
Thanks for your help
If I understand correctly, you would like to preserve a centre position after the zoom gestures. Zooming with the gesture doesn't maintain the same centre, you should correct position of camera once the gesture zoom is over. You can listen the idle event after zooming and animate the camera to the initial centre position.
Code snippet
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleMap.OnCameraIdleListener {
private GoogleMap map;
private LatLng requestedPosition;
#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);
}
#Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
// Add a marker in Sydney and move the camera
requestedPosition = new LatLng(41.385692,2.163953);
float zoom = 16.0f;
map.addMarker(new MarkerOptions().position(requestedPosition).title("Marker in Barcelona"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
//map.moveCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
// Disable all Ui interaction except for zoom
map.getUiSettings().setAllGesturesEnabled(false);
map.getUiSettings().setZoomGesturesEnabled(true);
map.setOnCameraIdleListener(this);
}
#Override
public void onCameraIdle() {
float zoom = map.getCameraPosition().zoom;
map.animateCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
}
}
I placed this sample at Github https://github.com/xomena-so/so43733628
Hope this helps!
When adding some markers on a map using Google Maps Android API v2, the markers are no set to the right positions and move when zooming.
When zooming in they get closer to the right positions, as if they were translated by a factor related to the zoom.
What's wrong?
Example zooming in:
fragment
public class CenterMapFragment extends Fragment {
private GoogleMap mMap;
private List<Center> mCenterList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.center_map_fragment, container, false);
}
#Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
mCenterList = MyApplication.dbHelper.getAllCenter();
for(Center center : mCenterList){
mMap.addMarker(new MarkerOptions().position(new LatLng(center.lat, center.lng)).icon(BitmapDescriptorFactory
.fromResource(R.drawable.myicon)));
}
}
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
For custom markers you need also use
MarkerOptions.anchor(float u, float v)
which describes where is the pointer point on the marker. By default this point is located at middle-bottom of the image.
To show an window info, you have to use setInfoWindowAdapter on your "mMap" or have a title set for your marker.
About the icon, it seems to be moving when you zoom out, but the anchor always point to the same place on the map. You can set an anchor if the image you're using don't follow Google's icon pattern (anchor on the middle of the botton of the image). For that you have to do use MarkerOptions' method anchor(u,v)
I am using the ArcGis for the first time in my android application(java).
I have got the X and Y co-ordinates of a location by "one map" http://www.onemap.sg/API/Help/RESTExamples.aspx,
now I want t display this point as a red dot in Map.
X-26005.0765
Y-30007.0973
Does anyone know any tutorial to follow. I saw the "Hello Map Tutorial".
Can Anyone direct me to the tutorial or documentation with example.
Asmita
You must use Graphics layer to add a graphical representation of your point.
The code below is an example were you print a point in the coordinates that you gaved.
public class Test extends Activity {
// View elements
MapView map;
GraphicsLayer measures = new GraphicsLayer();
Point point;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get elements of the view
map = (MapView) findViewById(R.id.map);
map.addLayer(measures);
Point point = new Point(26005.0765, 30007.0973);
measures.addGraphic(new Graphic(point,new SimpleMarkerSymbol(Color.RED,10,STYLE.CIRCLE)));
map.zoomTo(point, 0);
}
}
well, i'm developing a geolocation app in Android. On first run we center the map at the current location, then the user can zoom and pan freedomly, but we have a button that animates the map and centers it back to the actual position.
The problem is that this just happens when the map is static: if the user scrolls the map and leaves it scrolling by inertia, this button won't work until the animation is stopped.
Here's the code.
mapView.getController().stopAnimation(false); //this aint working as expected
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
Thanks.
This works for me:
public void centerCurrentClickHandler(View v) {
if (hasCurrentPosition) {
GeoPoint point = new GeoPoint(currentLatitudeE6, currentLongitudeE6);
mapController.animateTo(point);
}
}
public void centerFlagClickHandler(View v) {
if (hasPushpinPosition) {
GeoPoint point = new GeoPoint(pushpinLatitudeE6, pushpinLongitudeE6);
mapController.animateTo(point);
}
}