0

I want to use a map where both source and destination address will be pre-filled.

Like, I would prompt user to select current location, i.e, point A. ( I already did that) . Then, I will list up some places based on his/her location. Now, I want to take that address directly to the Destination Address , i.e, point B. and ,I will show to the user the route and exact Distance. Can You tell me how to prefix the Address for Point B, as I said above?

But, Both of the locations would be changed any time. for example,

My current location is BTM Layout, Bangalore , India (Source)and The Restaurant I want to go is in Koramangala, Bangalore, India(Destination). Later I changed my mind and want to go to another restaurant which is in Marathahalli, Bangalore, India(Destination). Let, In this case my source address also changed , its now HSR Layout, Bangalore, India.

My app will draw a map between this two location. Can anyone please help me out ? How to do it? Is it possible to get direction like this?

GuruCharan
  • 139
  • 1
  • 14
Sourav
  • 91
  • 1
  • 6
  • 12

1 Answers1

1

If your only problem is about the zoom, you can try something like this :

  1. Create to Location objets from your two points (assuming they are both in LatLng objets)

    // Start position
    Location locStart = new Location("");
    locStart.setLatitude(lngStart.latitude);
    locStart.setLongitude(lngStart.longitude);
    
    // End position
    Location locEnd = new Location("");
    locEnd.setLatitude(lngEnd.latitude);
    locEnd.setLongitude(lngEnd.longitude);
    
  2. Get the distance between your two points

    double distance = locStart.distanceTo(locEnd);
    
  3. Get the right zoom level

    The Maps API uses zoom levels (0 = far away ... 20 = buildings), so you have to found a way to switch between a distance (in meters) and a zoom level.

    int level;
    
    if (distance < 500) level = 1;
    else if (distance >= 500 && distance < 2000) level = 2;
    // and continue ...
    

    (values are for only the example, not tested)

  4. Get the virtual midpoint

    (so you can center your map)

    Check that answer, I'm not good in maths :)

  5. Update map

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(midpoint, level));
    

I hope it will help you :)

Community
  • 1
  • 1
Rascafr
  • 174
  • 11
  • Thanks for your answer. But can you explain the first point a little more. Actually, as I said my Source and Destination address is getting changed every time. Its easy to get the source( current location) , but how to add destination address automatically the map? – Sourav Mar 18 '16 at 15:34