0

I have an Android app for searching flights. I want to implement a Google Map where the Origin and the Destination will be shown in the map (with the help of some markers) to the user based on his/her inputs. But before I start anything, I need a help in the overall understanding.

  1. Which API do I need? Google Maps API or Google Maps Direction API? I don't want to show any direction, only the places marked.
  2. Is there any HTTP request for Google Maps API? Or how do I dynamically show the places in the map? I will only have the name of the places.

I would really appreciate if somebody can guide me through the initial steps of this implementation.


EDITED:

Below is the Main Activity that I am using:

public class Map_Activity_With_Fragment extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap googleMap;
String Origin, Destination, Start_Date, Num_Adult, Num_Infant, Origin_Map, Destination_Map;
Context context;
List<Address> Addr_Origin, Addr_Dest;
double latitude_origin, longitude_origin, latitude_destination, longitude_destination;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    Origin = extras.getString(MainActivity.ORIGIN);
    Destination = extras.getString(MainActivity.DESTINATION);
    Start_Date = extras.getString(MainActivity.START_DATE);
    Num_Adult = extras.getString(MainActivity.ADULT);
    Num_Infant = extras.getString(MainActivity.INFANT);
    Origin_Map = extras.getString(MainActivity.ORIGIN_MAP);
    Destination_Map = extras.getString(MainActivity.DESTINATION_MAP);
    context = Map_Activity_With_Fragment.this;
    setTitle("Location Map");
    setContentView(R.layout.activity_map__with__fragment);

    try
    {
        initializeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onMapReady(GoogleMap googleMap)
{
    AddMarkers(googleMap);
}


@Override
protected void onResume() {
    super.onResume();
    initializeMap();
}

private void initializeMap()
{
    if (googleMap == null) {
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
}

private void AddMarkers(GoogleMap googleMap)
{
    try {
        Geocoder geocoder = new Geocoder(context);
        Addr_Origin = geocoder.getFromLocationName(Origin_Map, 1);
        Addr_Dest = geocoder.getFromLocationName(Destination_Map, 1);
        if (Addr_Origin.size() > 0) {
            latitude_origin = Addr_Origin.get(0).getLatitude();
            longitude_origin = Addr_Origin.get(0).getLongitude();
        }
        if (Addr_Dest.size() > 0) {
            latitude_destination = Addr_Dest.get(0).getLatitude();
            longitude_destination = Addr_Dest.get(0).getLongitude();
        }
        Marker m1 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_origin, longitude_origin)).title(Origin_Map));
        Marker m2 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_destination, longitude_destination)).title(Destination_Map));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
Saumik Bhattacharya
  • 891
  • 1
  • 12
  • 28

1 Answers1

1
  1. Google Maps API should be enough.
  2. If you have location you can adds marker for each place https://developers.google.com/maps/documentation/android-api/marker

Get cooridnates: How to get coordinates of an address in android

Community
  • 1
  • 1
usil
  • 623
  • 1
  • 9
  • 20
  • Thanks @lobi! Is there any HTTPS request to invoke Google Maps API? Or How do I pass the coordinates to the map dynamically? – Saumik Bhattacharya Sep 21 '15 at 04:50
  • @AndroidBeginner what do you think with HTTPS request to invoke Google Maps API you should start with this https://developers.google.com/maps/documentation/android-api/intro – usil Sep 21 '15 at 05:56
  • @AndroidBeginner follow this step by step https://developers.google.com/maps/documentation/android-api/config – usil Sep 21 '15 at 05:57
  • I know the links that you have shared. In that, they have implemented a stand-alone app for Google Maps. In my case, I have an app which is searching flights, and on top of that I want to implement Google Maps. The one which you have mentioned is this the only way to go ahead? – Saumik Bhattacharya Sep 21 '15 at 06:28
  • I don't understand you. You sad that you like to show origin and destination on mpas. So where is the problem? – usil Sep 21 '15 at 07:22
  • yes I want to show Origin and Destination in Maps. Anyways I will try to implement whatever is mentioned over the links and will keep you updated. Thanks for the suggestion! – Saumik Bhattacharya Sep 21 '15 at 08:39
  • then you can up vote my answer, because this is what you need ;) – usil Sep 21 '15 at 14:06
  • I have implemented everything that was mentioned in those links. But still the Google Map is not getting rendered successfully. I am getting an error like my Google Play Services are not updated. But I have updated that in the SDK, the latest package is installed for that. Can you please help me in this? I have added the required code snippets in the edited part of the question. – Saumik Bhattacharya Sep 22 '15 at 18:51
  • Aah! I guess the emulator on which I am testing my code is not updated. In that case I have to test this directly on the device. I will let you know on this. – Saumik Bhattacharya Sep 23 '15 at 06:12
  • I have tested the code in the device directly and it worked! :) The map was rendered properly.:) Also I added a marker to the map. But I am having problem in putting multiple markers in the map. In that case none of the markers are visible. – Saumik Bhattacharya Sep 24 '15 at 16:22
  • I don't know why you have this problem. Did you try like this LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude()); map.addMarker(new MarkerOptions().position(newLatLng)); or check this link http://stackoverflow.com/questions/13855049/how-to-show-multiple-markers-on-mapfragment-in-google-map-api-v2 if this work for youm then you can accept the answer :) – usil Sep 24 '15 at 19:54
  • I am trying to make a simpler implementation of the markers. With the hardcoded value of latitude and longitude I am able to get multiple markers. But without hardcoding I am not able to get it. None of the markers are coming. Please see the code in the edited section of my question. There 'Origin_Map' and 'Destination_Map' are variables containing name of the places. Somehow I am not able to get the markers. Please help me out here.! – Saumik Bhattacharya Sep 25 '15 at 09:09
  • i have no idea. Try some Log.d(" " , "" + latitude_destination) – usil Sep 27 '15 at 14:07
  • This is finally working :) I was testing my code in a device of API level 16. Hence markers were not getting rendered successfully. Now I have tested my code in a device of API level 19, and I can see my changes working perfectly without any code change. :) – Saumik Bhattacharya Sep 29 '15 at 19:09