0% found this document useful (0 votes)
111 views6 pages

Document

The document defines an Android application that displays a map with markers for an origin and destination location. It implements Places API autocomplete to allow selecting a destination, and draws a route between the locations on the map.

Uploaded by

boratkarvansh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views6 pages

Document

The document defines an Android application that displays a map with markers for an origin and destination location. It implements Places API autocomplete to allow selecting a destination, and draws a route between the locations on the map.

Uploaded by

boratkarvansh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

<uses-permission android:name=”android.permission.

INTERNET” />
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
[26/03, 12:52 pm] +91 93737 01436: <RelativeLayout
xmlns:android=http://schemas.android.com/apk/res/android
Xmlns:tools=http://schemas.android.com/tools
Android:layout_width=”match_parent”
Android:layout_height=”match_parent”>

<fragment
Android:id=”@+id/map”
Android:name=”com.google.android.gms.maps.SupportMapFragment”
Android:layout_width=”match_parent”
Android:layout_height=”match_parent”
Tools:context=”.MainActivity” />

<fragment
Android:id=”@+id/autocomplete_fragment”

Android:name=”com.google.android.libraries.places.widget.AutocompleteSupportFragment”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content”
Android:layout_margin=”16dp”
Android:background=”@android:color/transparent”
Android:visibility=”visible” />

</RelativeLayout>
[26/03, 12:52 pm] +91 93737 01436: import android.os.Bundle;
Import android.widget.Toast;

Import androidx.annotation.NonNull;
Import androidx.appcompat.app.AppCompatActivity;

Import com.google.android.gms.common.api.Status;
Import com.google.android.gms.maps.CameraUpdateFactory;
Import com.google.android.gms.maps.GoogleMap;
Import com.google.android.gms.maps.OnMapReadyCallback;
Import com.google.android.gms.maps.SupportMapFragment;
Import com.google.android.gms.maps.model.LatLng;
Import com.google.android.gms.maps.model.MapStyleOptions;
Import com.google.android.gms.maps.model.MarkerOptions;
Import com.google.android.gms.maps.model.Polyline;
Import com.google.android.gms.maps.model.PolylineOptions;
Import com.google.android.libraries.places.api.Places;
Import com.google.android.libraries.places.api.model.Place;
Import com.google.android.libraries.places.api.model.TypeFilter;
Import com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest;
Import com.google.android.libraries.places.api.net.PlacesClient;
Import com.google.android.libraries.places.widget.AutocompleteSupportFragment;
Import com.google.android.libraries.places.widget.listener.AutocompleteCallback;
Import com.google.android.libraries.places.widget.model.AutocompletePrediction;
Import com.google.android.libraries.places.widget.model.AutocompleteSessionToken;
Import com.google.android.libraries.places.widget.model.RectangularBounds;

Import java.util.Arrays;
Import java.util.List;

Public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

Private GoogleMap mMap;


Private PlacesClient placesClient;
Private LatLng origin, destination;
Private Polyline polyline;

@Override
Protected void onCreate(Bundle savedInstanceState) {
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize Places API


If (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), getString(R.string.google_maps_key));
}
placesClient = Places.createClient(this);

// Initialize map fragment


SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

// Initialize AutocompleteSupportFragment
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME,
Place.Field.LAT_LNG));
autocompleteFragment.setTypeFilter(TypeFilter.ADDRESS);
autocompleteFragment.setOnPlaceSelectedListener(new AutocompleteCallback());
}

@Override
Public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Customize map styling


mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.map_style));

// Enable location tracking


mMap.setMyLocationEnabled(true);

// Set default location (e.g., your current location)


// origin = new LatLng(latitude, longitude);
// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(origin, 15));

// For demonstration purpose, adding default markers


Origin = new LatLng(40.7128, -74.0060); // New York City
Destination = new LatLng(34.0522, -118.2437); // Los Angeles
mMap.addMarker(new MarkerOptions().position(origin).title(“Origin”));
mMap.addMarker(new MarkerOptions().position(destination).title(“Destination”));
// Draw route between origin and destination
drawRoute();
}

Private void drawRoute() {


// Add polyline between origin and destination
List<LatLng> points = Arrays.asList(origin, destination);
PolylineOptions polylineOptions = new
PolylineOptions().addAll(points).color(getResources().getColor(R.color.polyline_color)).width(5
);
Polyline = mMap.addPolyline(polylineOptions);
}

Private class AutocompleteCallback extends


AutocompleteSupportFragment.OnPlaceSelectedListener {
@Override
Public void onPlaceSelected(@NonNull Place place) {
// Handle the selected place
Destination = place.getLatLng();
mMap.clear();
mMap.addMarker(new MarkerOptions().position(origin).title(“Origin”));
mMap.addMarker(new MarkerOptions().position(destination).title(“Destination”));
drawRoute();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(destination, 10));
}

@Override
Public void onError(@NonNull Status status) {
// Handle errors
Toast.makeText(MainActivity.this, “Error: “ + status.getStatusMessage(),
Toast.LENGTH_SHORT).show();
}
}
}

You might also like