Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps v2 - set both my location and zoom in

My question is, does anyone know how to set google maps up, to open up both my location and in a zoomed-in view?

Currently, the main view opens up to Africa, all the way zoomed out.

And so I have been searching for days now, and all I can find are:

1) You cannot animate two things (like zoom in and go to my location) in one google map? So if I can figure out how to set the zoom before I set the animate, then this problem would be solved. That tends to be the issue, you can change one, but not both.

2) I have found other classes that might be useful, but there is no help on how to set up the code so the class can manipulate the google map.

This is the code I have been holding on to so far, some works, some do not. Some I thought might be useful later on.

package com.MYWEBSITE.www;  import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu;  public class MainActivity extends FragmentActivity { private GoogleMap map;    @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main_layout);      map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();     map.setMyLocationEnabled(true);      //LocationSource a = (LocationSource) getSystemService(Context.LOCATION_SERVICE);     //LocationManager b = (LocationManager) getSystemService(Context.LOCATION_SERVICE);     //map.setLocationSource(a);      Criteria criteria = new Criteria();     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);     String provider = locationManager.getBestProvider(criteria, false);     Location location = locationManager.getLastKnownLocation(provider);     double lat =  location.getLatitude();     double lng = location.getLongitude();     LatLng coordinate = new LatLng(lat, lng);      //CameraPosition.Builder x = CameraPosition.builder();     //x.target(coordinate);     //x.zoom(13);      //Projection proj = map.getProjection();     //Point focus = proj.toScreenLocation(coordinate);      //map.animateCamera(CameraUpdateFactory.newLatLng(coordinate));     map.animateCamera(CameraUpdateFactory.zoomBy(13));     //map.moveCamera(CameraUpdateFactory.newLatLng(coordinate));       ////LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds; } } 
like image 777
Dustin Avatar asked Dec 28 '12 18:12

Dustin


People also ask

Can you use Google Maps and zoom at the same time?

Google Maps - cannot zoom and move at the same time with animateCamera, but can with moveCamera. Bookmark this question.

How do you zoom in and out on the map is done with?

The shortcut is simple: just double-tap the maps interface, but instead of lifting your finger after the second tap (the shortcut for zooming in), you leave it touching the screen. Then a swipe up zooms out, and a swipe down zooms in.

How do I zoom in and embed in Google Maps?

you can use zoom variable for zoom level like bellow. Visit Google maps google.com/maps to create map (Step by step: 1) Find location 2) Click the cog symbol in the top left corner and select "Share or embed map" 3) On modal window select "Embed map" 4) Copy iframe code and paste it).


1 Answers

You cannot animate two things (like zoom in and go to my location) in one google map?

From a coding standpoint, you would do them sequentially:

    CameraUpdate center=         CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,                                                  -73.98180484771729));     CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);      map.moveCamera(center);     map.animateCamera(zoom); 

Here, I move the camera first, then animate the camera, though both could be animateCamera() calls. Whether GoogleMap consolidates these into a single event, I can't say, as it goes by too fast. :-)

Here is the sample project from which I pulled the above code.


Sorry, this answer is flawed. See Rob's answer for a way to truly do this in one shot, by creating a CameraPosition and then creating a CameraUpdate from that CameraPosition.

like image 123
CommonsWare Avatar answered Sep 30 '22 20:09

CommonsWare