可是为什么改着改着import com.amap.api.maps.AMapUtils;说是Unused import statement了呢?目前代码如下:package com.example.bus.ui.map;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.constraintlayout.widget.ConstraintSet;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.example.bus.CityManager;
import com.example.bus.PoiIntelligenceUtils;
import com.example.bus.R;
import com.example.bus.RealTimePoiSuggestHelper;
import com.example.bus.RoutePlanActivity;
import com.example.bus.ResultAdapter;
import com.example.bus.databinding.FragmentMapBinding;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import com.amap.api.maps.model.BitmapDescriptorFactory;
public class MapFragment extends Fragment implements PoiSearch.OnPoiSearchListener, GeocodeSearch.OnGeocodeSearchListener {
private FragmentMapBinding binding;
private MapView mapView;
private AMap aMap;
private RealTimePoiSuggestHelper activeSuggestHelper;
private List<PoiItem> poiList = new ArrayList<>();
private ResultAdapter adapter;
private PoiSearch poiSearch;
private int selectionStage = 0;
private PoiItem selectedStartPoi = null;
private PoiItem selectedEndPoi = null;
private Marker startMarker = null;
private Marker endMarker = null;
private String lastStartKeyword = "";
private String lastEndKeyword = "";
private String currentCity = "";
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1001;
private boolean userHasInteracted = false;
private GeocodeSearch geocodeSearch;
private double myCurrentLat = 0;
private double myCurrentLng = 0;
private boolean isLocationReady = false;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = FragmentMapBinding.inflate(inflater, container, false);
View root = binding.getRoot();
mapView = binding.mapView;
mapView.onCreate(savedInstanceState);
initViews();
setupMap(savedInstanceState);
setupSearchSuggestion();
return root;
}
private void setupMap(Bundle savedInstanceState) {
mapView = binding.mapView;
mapView.onCreate(savedInstanceState);
aMap = mapView.getMap();
if (aMap != null) {
initMapSettings();
} else {
new Handler(Looper.getMainLooper()).post(() -> {
aMap = mapView.getMap();
if (aMap != null) {
initMapSettings();
} else {
waitAMapReady();
}
});
}
try {
geocodeSearch = new GeocodeSearch(requireContext());
geocodeSearch.setOnGeocodeSearchListener(this);
} catch (com.amap.api.services.core.AMapException e) {
e.printStackTrace();
Toast.makeText(requireContext(), "地理编码服务初始化失败", Toast.LENGTH_SHORT).show();
}
geocodeSearch.setOnGeocodeSearchListener(this);
}
private void waitAMapReady() {
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
int retry = 0;
@Override
public void run() {
if (mapView == null) return;
aMap = mapView.getMap();
if (aMap != null) {
initMapSettings();
} else if (retry++ < 30) {
new Handler(Looper.getMainLooper()).postDelayed(this, 100);
}
}
}, 100);
}
private void initMapSettings() {
UiSettings uiSettings = aMap.getUiSettings();
uiSettings.setZoomControlsEnabled(true);
uiSettings.setCompassEnabled(true);
uiSettings.setScrollGesturesEnabled(true);
uiSettings.setMyLocationButtonEnabled(true);
// 初始视野:中国中心
new Handler(Looper.getMainLooper()).post(() ->
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(35.8617, 104.1954), 4f))
);
enableMyLocationLayer();
}
private void initViews() {
adapter = new ResultAdapter(poiList, this::onPoiItemSelected);
binding.resultList.setLayoutManager(new LinearLayoutManager(requireContext()));
binding.resultList.setAdapter(adapter);
binding.mapSearch.setOnClickListener(v -> performSearch());
binding.btnSwitchTarget.setOnClickListener(v -> {
if (selectionStage == 1) {
showEndpointSelection(binding.mapInput2.getText().toString().trim());
} else if (selectionStage == 2) {
showStartpointSelection(binding.mapInput1.getText().toString().trim());
}
});
binding.btnGoTo.setOnClickListener(v -> {
if (selectedStartPoi != null && selectedEndPoi != null) {
Intent intent = new Intent(requireContext(), RoutePlanActivity.class);
intent.putExtra(RoutePlanActivity.EXTRA_SOURCE, RoutePlanActivity.SOURCE_FROM_MAP_DIRECT);
intent.putExtra("start_lat", selectedStartPoi.getLatLonPoint().getLatitude());
intent.putExtra("start_lng", selectedStartPoi.getLatLonPoint().getLongitude());
intent.putExtra("target_lat", selectedEndPoi.getLatLonPoint().getLatitude());
intent.putExtra("target_lng", selectedEndPoi.getLatLonPoint().getLongitude());
intent.putExtra("target_title", selectedEndPoi.getTitle());
startActivity(intent);
} else {
Toast.makeText(requireContext(), "请完成起点和终点的选择", Toast.LENGTH_SHORT).show();
}
});
}
private void performSearch() {
String startKeyword = binding.mapInput1.getText().toString().trim();
String endKeyword = binding.mapInput2.getText().toString().trim();
if (startKeyword.isEmpty()) {
Toast.makeText(requireContext(), "请输入起点", Toast.LENGTH_SHORT).show();
return;
}
if (endKeyword.isEmpty()) {
Toast.makeText(requireContext(), "请输入终点", Toast.LENGTH_SHORT).show();
return;
}
if (startKeyword.equals(lastStartKeyword) && endKeyword.equals(lastEndKeyword) && selectedStartPoi != null && selectedEndPoi != null) {
binding.btnGoTo.performClick();
return;
}
binding.containerResultList.setVisibility(View.VISIBLE);
binding.buttonGroup.setVisibility(View.VISIBLE);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(binding.getRoot());
constraintSet.connect(R.id.map_view, ConstraintSet.BOTTOM, R.id.container_result_list, ConstraintSet.TOP, 0);
constraintSet.applyTo(binding.getRoot());
userHasInteracted = true;
View currentFocus = requireActivity().getCurrentFocus();
if (currentFocus != null) {
currentFocus.clearFocus();
InputMethodManager imm = (InputMethodManager) requireContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
if (!startKeyword.equals(lastStartKeyword)) {
lastStartKeyword = startKeyword;
lastEndKeyword = endKeyword;
showStartpointSelection(startKeyword);
} else if (!endKeyword.equals(lastEndKeyword)) {
lastEndKeyword = endKeyword;
showEndpointSelection(endKeyword);
} else if (selectedStartPoi == null) {
showStartpointSelection(startKeyword);
} else {
showEndpointSelection(endKeyword);
}
}
private void showStartpointSelection(String keyword) {
selectionStage = 1;
binding.btnSwitchTarget.setText("前往选择终点");
binding.btnGoTo.setEnabled(false);
binding.emptyView.setText("🔍 搜索中...");
binding.emptyView.setVisibility(View.VISIBLE);
binding.resultList.setVisibility(View.GONE);
doSearch(keyword);
}
private void showEndpointSelection(String keyword) {
selectionStage = 2;
binding.btnSwitchTarget.setText("回到选择起点");
binding.btnGoTo.setEnabled(false);
binding.emptyView.setText("🔍 搜索中...");
binding.emptyView.setVisibility(View.VISIBLE);
binding.resultList.setVisibility(View.GONE);
doSearch(keyword);
}
private void doSearch(String keyword) {
if (keyword.isEmpty()) return;
CityManager.ParsedQuery parsed = CityManager.parse(keyword);
boolean isTargetCitySpecified = !parsed.targetCity.isEmpty();
// 判断是否为泛词 → 使用 dummy 查询
PoiSearch.Query dummyQuery = new PoiSearch.Query(parsed.keyword, "", currentCity);
PoiSearch tempSearch;
try {
tempSearch = new PoiSearch(requireContext(), dummyQuery);
tempSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
@Override
public void onPoiSearched(PoiResult result, int rCode) {
boolean isGeneric = (rCode == 1000 && result != null && result.getPois() != null)
? PoiIntelligenceUtils.isGenericCategory(result.getPois()) : false;
if (isGeneric) {
// 泛词:附近优先
PoiSearch.Query query = new PoiSearch.Query(parsed.keyword, "", currentCity);
query.setPageSize(20);
try {
poiSearch = new PoiSearch(requireContext(), query);
poiSearch.setOnPoiSearchListener(MapFragment.this);
if (isLocationReady) {
LatLonPoint center = new LatLonPoint(myCurrentLat, myCurrentLng);
poiSearch.setBound(new PoiSearch.SearchBound(center, 3000));
}
poiSearch.searchPOIAsyn();
} catch (com.amap.api.services.core.AMapException e) {
handleSearchError(-1);
e.printStackTrace();
}
} else {
// 非泛词:按规则走
if (isTargetCitySpecified) {
searchInSingleCity(parsed.keyword, parsed.targetCity);
} else {
searchInCurrentThenNationwide(parsed.keyword, currentCity);
}
}
}
@Override
public void onPoiItemSearched(PoiItem item, int rCode) {}
});
tempSearch.searchPOIAsyn();
} catch (com.amap.api.services.core.AMapException e) {
e.printStackTrace();
handleSearchError(-1);
}
}
private void searchInSingleCity(String keyword, String city) {
PoiSearch.Query query = new PoiSearch.Query(keyword, "", city);
query.setPageSize(50);
query.setPageNum(0);
try {
poiSearch = new PoiSearch(requireContext(), query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();
} catch (Exception e) {
handleSearchError(-1);
}
}
private void searchInCurrentThenNationwide(String keyword, String currentCity) {
binding.emptyView.setText("🔍 正在整合本地与全国结果...");
List<PoiItem> allResults = new ArrayList<>();
// 第一阶段:本地城市搜索
PoiSearch.Query localQuery = new PoiSearch.Query(keyword, "", currentCity);
PoiSearch localSearch;
try {
localSearch = new PoiSearch(requireContext(), localQuery);
localSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
@Override
public void onPoiSearched(PoiResult result, int rCode) {
if (rCode == 1000 && result != null && result.getPois() != null) {
allResults.addAll(result.getPois());
}
// 第二阶段:全国其他城市搜索
PoiSearch.Query nationalQuery = new PoiSearch.Query(keyword, "", "");
PoiSearch nationalSearch;
try {
nationalSearch = new PoiSearch(requireContext(), nationalQuery);
nationalSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
@Override
public void onPoiSearched(PoiResult res, int code) {
if (code == 1000 && res != null && res.getPois() != null) {
List<PoiItem> filtered = res.getPois().stream()
.filter(poi -> !currentCity.contains(poi.getCityName()))
.collect(Collectors.toList());
allResults.addAll(filtered);
}
// 去重并更新 UI
List<PoiItem> unique = new ArrayList<>(new HashSet<>(allResults));
updateResultList(unique);
}
@Override
public void onPoiItemSearched(PoiItem item, int rCode) {}
});
nationalSearch.searchPOIAsyn();
} catch (com.amap.api.services.core.AMapException e) {
e.printStackTrace();
// 即使全国搜索失败,也显示已有结果
updateResultList(allResults);
}
}
@Override
public void onPoiItemSearched(PoiItem item, int rCode) {}
});
localSearch.searchPOIAsyn();
} catch (com.amap.api.services.core.AMapException e) {
e.printStackTrace();
// 如果本地搜索都失败,尝试降级为全国搜索
searchInSingleCity(keyword, ""); // 空城市表示全国
}
}
private void onPoiItemSelected(PoiItem item) {
LatLng latLng = new LatLng(item.getLatLonPoint().getLatitude(), item.getLatLonPoint().getLongitude());
if (selectionStage == 1) {
if (startMarker != null) startMarker.remove();
startMarker = aMap.addMarker(new MarkerOptions()
.position(latLng)
.title("起点:" + item.getTitle())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
selectedStartPoi = item;
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14f));
} else if (selectionStage == 2) {
if (endMarker != null) endMarker.remove();
endMarker = aMap.addMarker(new MarkerOptions()
.position(latLng)
.title("终点:" + item.getTitle())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
selectedEndPoi = item;
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14f));
}
userHasInteracted = true;
updateGoToButtonState();
}
private void updateGoToButtonState() {
binding.btnGoTo.setEnabled(selectedStartPoi != null && selectedEndPoi != null);
}
@Override
public void onPoiSearched(PoiResult result, int rCode) {
// 这个回调不再使用,由内部双阶段接管
}
@Override
public void onPoiItemSearched(PoiItem item, int rCode) {}
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
if (rCode == 1000 && result != null && result.getRegeocodeAddress() != null) {
String city = result.getRegeocodeAddress().getCity();
currentCity = (city != null && !city.isEmpty()) ? city : result.getRegeocodeAddress().getProvince();
if (activeSuggestHelper != null) {
activeSuggestHelper.setCurrentCity(currentCity);
}
}
}
@Override
public void onGeocodeSearched(com.amap.api.services.geocoder.GeocodeResult geocodeResult, int i) {}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
if (!userHasInteracted) {
enableMyLocationLayer();
}
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroyView() {
super.onDestroyView();
mapView.onDestroy();
binding = null;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
private void setupSearchSuggestion() {
RealTimePoiSuggestHelper suggestHelper = new RealTimePoiSuggestHelper(requireContext());
suggestHelper.setCurrentCity(currentCity);
activeSuggestHelper = suggestHelper;
suggestHelper.setCallback(suggestions -> {
if (suggestions.length > 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(
requireContext(),
android.R.layout.simple_dropdown_item_1line,
suggestions
);
new Handler(Looper.getMainLooper()).post(() -> {
binding.mapInput1.setAdapter(adapter);
binding.mapInput2.setAdapter(adapter);
if (requireActivity().getCurrentFocus() == binding.mapInput1) {
binding.mapInput1.showDropDown();
} else if (requireActivity().getCurrentFocus() == binding.mapInput2) {
binding.mapInput2.showDropDown();
}
});
}
});
if (isLocationReady) {
suggestHelper.setLocationBias(myCurrentLat, myCurrentLng);
}
Handler handler = new Handler(Looper.getMainLooper());
Runnable[] pending1 = {null}, pending2 = {null};
binding.mapInput1.addTextChangedListener(new SimpleTextWatcher(s -> {
if (pending1[0] != null) handler.removeCallbacks(pending1[0]);
if (s.length() == 0) {
binding.mapInput1.setAdapter(null);
return;
}
pending1[0] = () -> {
CityManager.ParsedQuery parsed = CityManager.parse(s.toString());
if (!parsed.targetCity.isEmpty()) {
suggestHelper.requestSuggestionsForCity(parsed.keyword, parsed.targetCity);
} else {
suggestHelper.requestSuggestions(parsed.keyword);
}
};
handler.postDelayed(pending1[0], 300);
}));
binding.mapInput2.addTextChangedListener(new SimpleTextWatcher(s -> {
if (pending2[0] != null) handler.removeCallbacks(pending2[0]);
if (s.length() == 0) {
binding.mapInput2.setAdapter(null);
return;
}
pending2[0] = () -> {
CityManager.ParsedQuery parsed = CityManager.parse(s.toString());
if (!parsed.targetCity.isEmpty()) {
suggestHelper.requestSuggestionsForCity(parsed.keyword, parsed.targetCity);
} else {
suggestHelper.requestSuggestions(parsed.keyword);
}
};
handler.postDelayed(pending2[0], 300);
}));
binding.mapInput1.setOnEditorActionListener((v, actionId, event) -> {
if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
});
binding.mapInput2.setOnEditorActionListener((v, actionId, event) -> {
if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
});
}
private void handleSearchError(int rCode) {
String msg;
switch (rCode) {
case 12: msg = "API Key 错误"; break;
case 27: msg = "网络连接失败"; break;
case 30: msg = "SHA1 或包名错误"; break;
case 33: msg = "请求频繁"; break;
default: msg = "搜索失败: " + rCode; break;
}
Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show();
}
private void updateResultList(List<PoiItem> list) {
poiList.clear();
poiList.addAll(list);
adapter.notifyDataSetChanged();
binding.resultList.scrollToPosition(0);
binding.emptyView.setVisibility(View.GONE);
binding.resultList.setVisibility(View.VISIBLE);
if (!list.isEmpty()) {
adapter.setSelected(0);
onPoiItemSelected(list.get(0));
}
}
private LatLng toLatLng(LatLonPoint point) {
if (point == null) return null;
return new LatLng(point.getLatitude(), point.getLongitude());
}
private void enableMyLocationLayer() {
if (aMap == null) return;
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
MyLocationStyle myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
aMap.setMyLocationStyle(myLocationStyle);
aMap.setMyLocationEnabled(true);
AMap.OnMyLocationChangeListener listener = location -> {
if (location != null && !userHasInteracted) {
LatLng curLatlng = new LatLng(location.getLatitude(), location.getLongitude());
if (activeSuggestHelper != null) {
activeSuggestHelper.setLocationBias(location.getLatitude(), location.getLongitude());
}
myCurrentLat = location.getLatitude();
myCurrentLng = location.getLongitude();
isLocationReady = true;
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(curLatlng, 16f));
userHasInteracted = true;
LatLonPoint point = new LatLonPoint(myCurrentLat, myCurrentLng);
RegeocodeQuery query = new RegeocodeQuery(point, 200, GeocodeSearch.AMAP);
try {
geocodeSearch.getFromLocationAsyn(query);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(requireContext(), "逆地理编码请求失败", Toast.LENGTH_SHORT).show();
}
aMap.setOnMyLocationChangeListener(null);
}
};
aMap.setOnMyLocationChangeListener(listener);
} else {
ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (aMap != null) {
MyLocationStyle myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
aMap.setMyLocationStyle(myLocationStyle);
aMap.setMyLocationEnabled(true);
AMap.OnMyLocationChangeListener listener = location -> {
if (location != null && !userHasInteracted) {
LatLng curLatlng = new LatLng(location.getLatitude(), location.getLongitude());
if (activeSuggestHelper != null) {
activeSuggestHelper.setLocationBias(location.getLatitude(), location.getLongitude());
}
myCurrentLat = location.getLatitude();
myCurrentLng = location.getLongitude();
isLocationReady = true;
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(curLatlng, 16f));
userHasInteracted = true;
LatLonPoint point = new LatLonPoint(myCurrentLat, myCurrentLng);
RegeocodeQuery query = new RegeocodeQuery(point, 200, GeocodeSearch.AMAP);
try {
geocodeSearch.getFromLocationAsyn(query);
} catch (Exception e) {
e.printStackTrace();
}
aMap.setOnMyLocationChangeListener(null);
}
};
aMap.setOnMyLocationChangeListener(listener);
}
}
}
}
private static class SimpleTextWatcher implements android.text.TextWatcher {
private final java.util.function.Consumer<CharSequence> onTextChanged;
public SimpleTextWatcher(java.util.function.Consumer<CharSequence> onTextChanged) {
this.onTextChanged = onTextChanged;
}
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override public void afterTextChanged(Editable s) {}
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
onTextChanged.accept(s);
}
}
}
最新发布