Android, how to pass lat long route info to Google Maps App
Asked Answered
C

4

5

I have an Android app which shows a number of locations on a Map. When I click on a location I would like to pass the lat long of the location, and the lat long of the device's location, to the Google Maps App so that it can show me route information between the two.

Is this possible? If so how? Thanks.

Caducous answered 8/3, 2017 at 17:0 Comment(0)
A
6

Yes, You can pass lat and lng to the Maps , and show directions to that Place using the Android Intent.

Directions are always given from the users current location.

Following query will help you perform that . You can pass the destination latitude and longitude here:

google.navigation:q=latitude,longitude

Use above as:

Uri gmmIntentUri = Uri.parse("google.navigation:q=latitude,longitude");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

More Info here: https://developers.google.com/maps/documentation/urls/android-intents

Adroit answered 7/8, 2017 at 18:22 Comment(0)
M
4

As of 2017 the recommended by Google method is the Google Maps URLs:

https://developers.google.com/maps/documentation/urls/guide#directions-action

You can create a cross-platform universal URL for Google directions following the documentation and use the URL in your intents. For example the URL might be something like

https://www.google.com/maps/dir/?api=1&destination=60.626200,16.776800&travelmode=driving

googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
    @Override
    public void onMapClick(LatLng latLng)
    {   
        String url = "https://www.google.com/maps/dir/?api=1&destination=" + latLng.latitude + "," + latLng.longitude + "&travelmode=driving";           
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
    }
});

Hope this helps!

Monophagous answered 7/8, 2017 at 19:15 Comment(0)
A
2

You can try with this:

googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
    @Override
    public void onMapClick(LatLng latLng)
    {   
        String url = "http://maps.google.com/maps?daddr=" + latLng.latitude + "," + latLng.longitude;           
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
    }
});
Assr answered 8/3, 2017 at 17:19 Comment(0)
L
1

add dependency compile 'com.google.android.gms:play-services-maps:11.0.4'

add in manifest file

<permission
        android:name="com.example.com.locationmap.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

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

    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />     and meta data


 <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCHrUMMMFTLannfAaqQ9RGVaTu301Ee0j8" />

code for xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

code for java class

  public class MapActivity extends FragmentActivity implements 
        OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
    private GoogleMap mMap;
    double latitude = 0;
    double longitude = 0;
    double latitude2 = 0;
    double longitude2 = 0;
    LatLng position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        latitude = getIntent().getDoubleExtra("lat", 0);
        longitude = getIntent().getDoubleExtra("long", 0);
        // Getting Reference to SupportMapFragment of activity_map.xml
        SupportMapFragment fm = (SupportMapFragment) 
       getSupportFragmentManager().findFragmentById(R.id.map);
        fm.getMapAsync(MapActivity.this);

        }

    @Override
    public void onConnected(Bundle bundle) {
       /* getDirection();*/
        /*getCurrentLocation();*/
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

      @Override
    public void onMapReady(GoogleMap googleMap) {
        // Receiving latitude from MainActivity screen
        latitude = getIntent().getDoubleExtra("lat", 0);

        // Receiving longitude from MainActivity screen
        longitude = getIntent().getDoubleExtra("long", 0);


        latitude2 = 73.998;

        // Receiving longitude from MainActivity screen
        longitude2 = 22.4567;

        position = new LatLng(latitude, longitude);

        // Instantiating MarkerOptions class
        MarkerOptions options = new MarkerOptions();

        // Setting position for the MarkerOptions
        options.position(position);

        // Setting title for the MarkerOptions
        options.title("Location");

        // Setting snippet for the MarkerOptions
        // Adding Marker on the Google Map
        try {
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(latitude, 
       longitude, 1);
            String cityName = addresses.get(0).getAddressLine(0);
            String stateName = addresses.get(0).getAddressLine(1);
            String countryName = addresses.get(0).getAddressLine(2);
            options.snippet(cityName + "," + stateName);

        } catch (Exception ex) {
            ex.getMessage();
            options.snippet("Latitude:" + latitude + ",Longitude:" + longitude);

        }
        googleMap.addMarker(options);
        googleMap.addMarker(options.position(new LatLng(latitude2, 
        longitude2)));

        // Creating CameraUpdate object for position
        CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);

        // Creating CameraUpdate object for zoom
        CameraUpdate updateZoom = CameraUpdateFactory.zoomTo(6);

        // Updating the camera position to the user input latitude and longitude
        googleMap.moveCamera(updatePosition);

        // Applying zoom to the marker position
        googleMap.animateCamera(updateZoom);


        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
      /*
        getMenuInflater().inflate(R.menu.main, menu);
      */
        return true;
    }

    }
     `
Leelah answered 11/5, 2018 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.