-2

I am developing an android google map application, that is showing current location on map starting.

enter image description here

I have an search bar in the application, when user enter any area name, then the second marker will be placed on that location.

enter image description here

Now my problem is, how to get second marker longitude and latitude position and make a route between the two markers.

my MainActivity.java code as follows:

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    public void onMapSearch(View view) {
        EditText locationSearch = (EditText) findViewById(R.id.editText);
        String location = locationSearch.getText().toString();
        List<Address> addressList = null;

        if (location != null || !location.equals("")) {
            Geocoder geocoder = new Geocoder(this);
            try {
                addressList = geocoder.getFromLocationName(location, 1);

            } catch (IOException e) {
                e.printStackTrace();
            }
            Address address = addressList.get(0);
            LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
            mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
            mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(27.746974, 85.301582);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Kathmandu, Nepal"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        // Enable MyLocation Button in the Map
        mMap.setMyLocationEnabled(true);
    }
}

Please Help me.

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
Vamsi Abbineni
  • 479
  • 3
  • 7
  • 23

2 Answers2

0

In your program you have used the code to get the LatLng of the second marker.

LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));

To make a polyline, refer to this link. By the this question has already been answered.

https://www.simplifiedcoding.net/google-maps-distance-calculator-google-maps-api/

Geet Choubey
  • 1,069
  • 7
  • 23
0

Set Google Places API for search EdiText. Get the value from that and pass it as address..

Here is the code(address denoting strAddress here)

    List<Address> address = null;
    Geocoder coder = new Geocoder(getApplicationContext());
    try {
        address = coder.getFromLocationName(strAddress, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (address != null) {
        Address location = address.get(0);
        double lat = location.getLatitude();
        double log = location.getLongitude();
    }
karthickraja
  • 214
  • 1
  • 7