高德

高德
所有功能 官网都有解释
导入依赖

implementation ‘com.amap.api:map2d:latest.integration’
implementation ‘com.amap.api:search:latest.integration’
1
布局

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edit_bg"
    android:hint="我的位置"
    android:layout_marginTop="3dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="60dp"
    android:paddingLeft="5dp"
    android:id="@+id/my_location"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_bg"
        android:hint="终点"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="3dp"
        android:paddingLeft="5dp"
        android:layout_weight="1"
        android:id="@+id/my_destination"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="搜索"
        android:layout_weight="6"
        android:gravity="center"
        android:id="@+id/my_search"
        android:background="@drawable/txt_bg"
        android:layout_marginLeft="3dp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="步行"
        android:padding="3dp"
        android:background="@drawable/txt_bg"
        android:layout_marginLeft="30dp"
        android:id="@+id/walk"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="驾车"
        android:padding="3dp"
        android:background="@drawable/txt_bg"
        android:id="@+id/driver"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="公交"
        android:padding="3dp"
        android:background="@drawable/txt_bg"
        android:id="@+id/bus"/>
</LinearLayout>
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="3dp"
    android:divider="#B9B6B6"
    android:visibility="gone"
    android:id="@+id/my_list">

</ListView>
<com.amap.api.maps2d.MapView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_MapView">

</com.amap.api.maps2d.MapView>

list的Item布局

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/item_txt"
    android:textSize="18sp"/>
1 2 3 4 5 6 7 8 9 10 11 适配器

public class MyListAdapter extends BaseAdapter {

ArrayList<PoiItem> arrayList;
Context context;

public MyListAdapter(ArrayList<PoiItem> arrayList, Context context) {
    this.arrayList = arrayList;
    this.context = context;
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int position) {
    return arrayList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null){
        viewHolder = new ViewHolder();
        convertView = View.inflate(context,R.layout.list_item,null);
        viewHolder.textView = convertView.findViewById(R.id.item_txt);
        convertView.setTag(viewHolder);
    }else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.textView.setText(arrayList.get(position).getTitle());
    return convertView;
}

}
class ViewHolder{
TextView textView;
}

activity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

EditText my_location,my_destination;
TextView search,walk,bus,driver;
ListView listView;
MapView mapView;
String[] permission = {Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.ACCESS_WIFI_STATE,
                        Manifest.permission.ACCESS_NETWORK_STATE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.INTERNET
                        };
AMap aMap;
MyLocationStyle myLocationStyle;
ArrayList<PoiItem> arrayList = new ArrayList<>();
MyListAdapter myListAdapter;
LatLonPoint start;
LatLonPoint end;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (Build.VERSION.SDK_INT >= 23){
        for (int i = 0; i <permission.length; i++){
            int j = ContextCompat.checkSelfPermission(this, permission[i]);
            if (j == PackageManager.PERMISSION_DENIED){
                requestPermissions(permission,80);
            }
        }
    }
    initView();
    mapView.onCreate(savedInstanceState);
    myLocationStyle = new MyLocationStyle();
    myLocationStyle.interval(1000);
    myLocationStyle.showMyLocation(true);
    aMap.setMyLocationStyle(myLocationStyle);
    aMap.setMyLocationEnabled(true);
    myListAdapter = new MyListAdapter(arrayList,this);
    listView.setAdapter(myListAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            listView.setVisibility(View.GONE);
            aMap.clear();
            PoiItem poiItem = arrayList.get(position);
            LatLonPoint latLonPoint = poiItem.getLatLonPoint();
            end = latLonPoint;
            double latitude = latLonPoint.getLatitude();
            double longitude = latLonPoint.getLongitude();
            LatLng latLng = new LatLng(latitude,longitude);
            aMap.addMarker(new MarkerOptions().position(latLng));
        }
    });
}


private void initView() {
    my_location = findViewById(R.id.my_location);
    my_destination = findViewById(R.id.my_destination);
    search = findViewById(R.id.my_search);
    search.setOnClickListener(this);
    walk = findViewById(R.id.walk);
    walk.setOnClickListener(this);
    bus = findViewById(R.id.bus);
    bus.setOnClickListener(this);
    driver = findViewById(R.id.driver);
    driver.setOnClickListener(this);
    listView = findViewById(R.id.my_list);
    mapView = findViewById(R.id.my_MapView);
    if (mapView != null){
        aMap = mapView.getMap();
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.my_search:
            String placeName = my_destination.getText().toString().trim();
            if (placeName.isEmpty()){
                Toast.makeText(this, "终点不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            arrayList.clear();
            PoiSearch.Query query = new PoiSearch.Query(placeName,"","");
            PoiSearch poiSearch = new PoiSearch(this,query);
            poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                @Override
                public void onPoiSearched(PoiResult poiResult, int i) {
                    if (i == 1000 && poiResult != null){
                        ArrayList<PoiItem> pois = poiResult.getPois();
                        arrayList.addAll(pois);
                        myListAdapter.notifyDataSetChanged();
                        listView.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onPoiItemSearched(PoiItem poiItem, int i) {

                }
            });
            poiSearch.searchPOIAsyn();

            break;
        case R.id.walk:
            String placeName3 = my_location.getText().toString().trim();
            if (placeName3.isEmpty()){
                RouteSearch routeSearch = new RouteSearch(this);
                double latitude = aMap.getMyLocation().getLatitude();
                double longitude = aMap.getMyLocation().getLongitude();
                start = new LatLonPoint(latitude,longitude);
                RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(start,end);
                RouteSearch.WalkRouteQuery query1 = new RouteSearch.WalkRouteQuery(fromAndTo,RouteSearch.WALK_DEFAULT);
                routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
                    @Override
                    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

                    }

                    @Override
                    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {

                    }

                    @Override
                    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
                        Toast.makeText(MainActivity.this, ""+start.getLatitude(), Toast.LENGTH_SHORT).show();
                        Toast.makeText(MainActivity.this, ""+end.getLatitude(), Toast.LENGTH_SHORT).show();

                        if (i == 1000 && walkRouteResult != null){
                            List<WalkPath> paths = walkRouteResult.getPaths();
                            aMap.clear();
                            for (int j = 0; j < paths.size(); j++){
                                WalkRouteOverlay overlay = new WalkRouteOverlay(MainActivity.this,aMap,paths.get(j),start,end);
                                overlay.removeFromMap();
                                overlay.addToMap();
                                overlay.zoomToSpan();
                            }
                        }
                    }

                    @Override
                    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

                    }
                });
                routeSearch.calculateWalkRouteAsyn(query1);
            }else {
                RouteSearch routeSearch = new RouteSearch(this);
                PoiSearch.Query query2 = new PoiSearch.Query(placeName3,"","");
                PoiSearch poiSearch1 = new PoiSearch(this,query2);
                poiSearch1.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                    @Override
                    public void onPoiSearched(PoiResult poiResult, int i) {
                        if (i == 1000 && poiResult != null){
                            ArrayList<PoiItem> pois = poiResult.getPois();
                            LatLonPoint latLonPoint = pois.get(0).getLatLonPoint();
                            double latitude = latLonPoint.getLatitude();
                            double longitude = latLonPoint.getLongitude();
                            start = new LatLonPoint(latitude,longitude);
                        }
                    }

                    @Override
                    public void onPoiItemSearched(PoiItem poiItem, int i) {

                    }
                });
                poiSearch1.searchPOIAsyn();

                RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(start,end);
                RouteSearch.WalkRouteQuery query1 = new RouteSearch.WalkRouteQuery(fromAndTo,RouteSearch.WALK_DEFAULT);
                routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
                    @Override
                    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

                    }

                    @Override
                    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {

                    }

                    @Override
                    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
                        if (i == 1000 && walkRouteResult != null){
                            List<WalkPath> paths = walkRouteResult.getPaths();
                            aMap.clear();
                            for (int j = 0; j < paths.size(); j++){
                                WalkRouteOverlay overlay = new WalkRouteOverlay(MainActivity.this,aMap,paths.get(j),start,end);
                                overlay.removeFromMap();
                                overlay.addToMap();
                                overlay.zoomToSpan();
                            }
                        }
                    }

                    @Override
                    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

                    }
                });
                routeSearch.calculateWalkRouteAsyn(query1);
            }
            break;
        case  R.id.driver:
            String placeName1 = my_location.getText().toString().trim();
            if (placeName1.isEmpty()){
                RouteSearch routeSearch = new RouteSearch(this);
                double latitude = aMap.getMyLocation().getLatitude();
                double longitude = aMap.getMyLocation().getLongitude();
                start = new LatLonPoint(latitude,longitude);
                RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(start,end);
                RouteSearch.DriveRouteQuery query1 = new RouteSearch.DriveRouteQuery(fromAndTo,0,null,null,"");
                routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
                    @Override
                    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

                    }

                    @Override
                    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
                        if (i == 1000 && driveRouteResult != null){
                            List<DrivePath> paths = driveRouteResult.getPaths();
                            aMap.clear();
                            for (int j = 0; j < paths.size(); j++){
                                DrivingRouteOverlay overlay = new DrivingRouteOverlay(MainActivity.this,aMap,paths.get(j),start,end);
                                overlay.removeFromMap();
                                overlay.addToMap();
                                overlay.zoomToSpan();
                            }
                        }
                    }

                    @Override
                    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

                    }

                    @Override
                    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

                    }
                });
                routeSearch.calculateDriveRouteAsyn(query1);
            }else {
                RouteSearch routeSearch = new RouteSearch(this);


                PoiSearch.Query query2 = new PoiSearch.Query(placeName1,"","");
                PoiSearch poiSearch1 = new PoiSearch(this,query2);
                poiSearch1.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                    @Override
                    public void onPoiSearched(PoiResult poiResult, int i) {
                        if (i == 1000 && poiResult != null){
                            ArrayList<PoiItem> pois = poiResult.getPois();
                            LatLonPoint latLonPoint = pois.get(0).getLatLonPoint();
                            double latitude = latLonPoint.getLatitude();
                            double longitude = latLonPoint.getLongitude();
                            start = new LatLonPoint(latitude,longitude);
                        }
                    }

                    @Override
                    public void onPoiItemSearched(PoiItem poiItem, int i) {

                    }
                });
                poiSearch1.searchPOIAsyn();

                RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(start,end);
                RouteSearch.DriveRouteQuery query1 = new RouteSearch.DriveRouteQuery(fromAndTo,0,null,null,"");
                routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
                    @Override
                    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

                    }

                    @Override
                    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
                        if (i == 1000 && driveRouteResult != null){
                            List<DrivePath> paths = driveRouteResult.getPaths();
                            aMap.clear();
                            for (int j = 0; j < paths.size(); j++){
                                DrivingRouteOverlay overlay = new DrivingRouteOverlay(MainActivity.this,aMap,paths.get(j),start,end);
                                overlay.removeFromMap();
                                overlay.addToMap();
                                overlay.zoomToSpan();
                            }
                        }
                    }

                    @Override
                    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

                    }

                    @Override
                    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

                    }
                });
                routeSearch.calculateDriveRouteAsyn(query1);
            }
            break;
        case R.id.bus:
            String placeName2 = my_location.getText().toString().trim();
            if (placeName2.isEmpty()){
                RouteSearch routeSearch = new RouteSearch(this);
                double latitude = aMap.getMyLocation().getLatitude();
                double longitude = aMap.getMyLocation().getLongitude();
                start = new LatLonPoint(latitude,longitude);
                RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(start,end);
                RouteSearch.BusRouteQuery query1 = new RouteSearch.BusRouteQuery(fromAndTo,0,"北京",1);
                routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
                    @Override
                    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
                        if (i == 1000 && busRouteResult != null){
                            List<BusPath> paths = busRouteResult.getPaths();
                            aMap.clear();
                            for (int j = 0; j < paths.size(); j++){
                                BusRouteOverlay overlay = new BusRouteOverlay(MainActivity.this,aMap,paths.get(j),start,end);
                                overlay.removeFromMap();
                                overlay.addToMap();
                                overlay.zoomToSpan();
                            }
                        }
                    }

                    @Override
                    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {

                    }

                    @Override
                    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

                    }

                    @Override
                    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

                    }
                });
                routeSearch.calculateBusRouteAsyn(query1);
            }else {
                RouteSearch routeSearch = new RouteSearch(this);
                PoiSearch.Query query2 = new PoiSearch.Query(placeName2,"","");
                PoiSearch poiSearch1 = new PoiSearch(this,query2);
                poiSearch1.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                    @Override
                    public void onPoiSearched(PoiResult poiResult, int i) {
                        if (i == 1000 && poiResult != null){
                            ArrayList<PoiItem> pois = poiResult.getPois();
                            LatLonPoint latLonPoint = pois.get(0).getLatLonPoint();
                            double latitude = latLonPoint.getLatitude();
                            double longitude = latLonPoint.getLongitude();
                            start = new LatLonPoint(latitude,longitude);
                        }
                    }

                    @Override
                    public void onPoiItemSearched(PoiItem poiItem, int i) {

                    }
                });
                poiSearch1.searchPOIAsyn();

                RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(start,end);
                RouteSearch.BusRouteQuery query1 = new RouteSearch.BusRouteQuery(fromAndTo,0,"北京",1);
                routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
                    @Override
                    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
                        if (i == 1000 && busRouteResult != null){
                            List<BusPath> paths = busRouteResult.getPaths();
                            aMap.clear();
                            for (int j = 0; j < paths.size(); j++){
                                BusRouteOverlay overlay = new BusRouteOverlay(MainActivity.this,aMap,paths.get(j),start,end);
                                overlay.removeFromMap();
                                overlay.addToMap();
                                overlay.zoomToSpan();
                            }
                        }
                    }

                    @Override
                    public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {

                    }

                    @Override
                    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

                    }

                    @Override
                    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

                    }
                });
                routeSearch.calculateBusRouteAsyn(query1);
            }
            break;
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mapView != null){
        mapView.onDestroy();
    }
}

@Override
protected void onResume() {
    super.onResume();
    if (mapView != null){
        mapView.onResume();
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (mapView != null){
        mapView.onPause();
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值