Set coordinates when opening map

Asked

Viewed 2,456 times

3

I need to open a map with specific coordinates (latitude and longitude). These data I already have, but I don’t know how to set them on the map.

PS: It is possible to open directly in Google Maps?

  • Your question is to open the application Google Maps with this specific coordinate or yourself implement the map in your application? For the second option, I believe that this issue will answer you.

  • Would you like to open directly on 'Google Maps', as I continue? - Paulo Rodrigues

  • Caesar, with the answers below, I now have another question that seems not to be in your question. You want to open Google Maps is from your native app, right? Or you have a mobile site?

  • Native app! (from Android Studio)

4 answers

2

To open the Google Maps directly from your application with a certain coordinate, using Intent even, for example.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:-19.8516098,-43.9509601?z=15"));
startActivity(intent);

Other examples, you can see here in the documentation. In my example, in addition to latitude and longitude, I also defined the zoom.

1

Todo Marker, you have to insert via JS.

As you already have the position, just send to javascript and pick up a variable.

Or set direct as in this example from W3schools.

W3schools

This link below is from the official google maps documentation.

Google Maps

1

In your main activity, implement the interface Locationlistener and copy the code below in your main activity.

public class BasicMapActivity_new extends Activity implements LocationListener {
        private LocationManager locationManager;
        private String provider;

        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean enabledGPS = service
        .isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean enabledWiFi = service
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!enabledGPS) {
            Toast.makeText(BasicMapActivity_new.this, "GPS signal not found", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
        else if(!enabledWiFi){
            Toast.makeText(BasicMapActivity_new.this, "Network signal not found", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);
        //getCurrentLocation();

        // Initialize the location fields
        if (location != null) {
            // Toast.makeText(BasicMapActivity_new.this, "Selected Provider " + provider,
            //Toast.LENGTH_SHORT).show();
            onLocationChanged(location);
        } else {

            //do something
        }
        initilizeMap();
    }

Then in implementing the methods of that interface do the following:

   @Override
    public void onLocationChanged(Location location) {

        double lat =  location.getLatitude();
        double lng = location.getLongitude();

        LatLng coordinate = new LatLng(lat, lng);

        startPerc = mMap.addMarker(new MarkerOptions()
        .position(coordinate)
        .title("Current Location")
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));  

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 18.0f));
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
        initilizeMap();
    }

Finally grant the necessary permissions in the maninfest file:

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

0

just call CameraUpdateFactory.newLatLng(LatLng) when loading the map, or where necessary. Build a Latlng object that has the x and y point and you’re done.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.