1

I am trying to get google maps to zoom to the location of the device being used.

google maps is launched from inside my application via an intent the code is below.

button_bakery = (Button) findViewById(R.id.Breakfast_bakery);
    button_bakery.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View views) {
                    Button b = button_bakery;
                    String buttonText = b.getText().toString();

                    // Search for restaurants nearby
                    Uri gmmIntentUri = Uri.parse("geo:0,0?q=" + buttonText);
                    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                    mapIntent.setPackage("com.google.android.apps.maps");
                    startActivity(mapIntent);
                }
            }
    );

however this loads the location as the last location set by the device when using this application. Is there a way to set it to zoom to the current location of the device before the search is completed?

Thanks in advance!

bdg
  • 465
  • 6
  • 18

1 Answers1

4

By following this documentation, you can use the geo: intent to display the zoom level that you want.

geo:latitude,longitude?z=zoom

The z parameter sets the initial zoom level of the map. Accepted values range from 0 (the whole world) to 21 (individual buildings).

Here is a example code that use zoom level equal to 10(z=10) that will attemp to find restaurant at a city level.

Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194?z=10&q=restaurants");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

Also check this SO question for more information.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31