简体   繁体   English

在Android App中使用Google Maps API / Google Maps Directions API

[英]Using Google Maps API/Google Maps Directions API in Android App

I have an Android app for searching flights. 我有一个用于搜索航班的Android应用。 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. 我想实现一个Google Map,其中基于用户的输入(在某些标记的帮助下)向用户显示起点和终点。 But before I start anything, I need a help in the overall understanding. 但是,在开始任何事情之前,我需要帮助您进行总体理解。

  1. Which API do I need? 我需要哪个API? Google Maps API or Google Maps Direction API? Google Maps API或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? Google Maps API是否有任何HTTP请求? 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();
    }
}
}
  1. Google Maps API should be enough. Google Maps API应该足够了。
  2. If you have location you can adds marker for each place https://developers.google.com/maps/documentation/android-api/marker 如果您有位置,则可以为每个位置添加标记https://developers.google.com/maps/documentation/android-api/marker

Get cooridnates: How to get coordinates of an address in android 获取坐标: 如何在android中获取地址的坐标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM