该示例主要展示出行类app乘客端用户在设置起点和终点的方案。
核心类/接口
类 | 接口 | 说明 | 版本 |
---|---|---|---|
MAMapView | setShowsUserLocation | 开启定位 | V1.0.0 |
AMapSearchAPI | AMapPOIKeywordsSearch | POI关键字查询 | V1.0.0 |
AMapSearchAPI | AMapReGoecodeSearch | 逆地理编码查询 | V1.0.0 |
AMapSearchAPI | AMapGeocodeSearch | 地理编码查询 | V1.0.0 |
// 显示搜索&城市列表
- (void)showCityListViewOnlyCity:(BOOL)onlyCity
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.cityListView reset];
self.searchResultView.poiArray = nil;
self.searchBar.doubleSearchModeEnable = !onlyCity;
self.searchBar.seachCity = [MyCityManager sharedInstance].currentCity;
self.searchResultView.hidden = onlyCity;
if (!onlyCity) {
[self updateSearchResultForCurrentCity];
}
[self.searchBar reset];
[self.searchBar becomeFirstResponder];
[UIView animateWithDuration:0.3 animations:^{
self.listContainerView.frame = CGRectMake(kTableViewMargin, kTableViewMargin + kNaviBarHeight, self.listContainerView.frame.size.width, self.listContainerView.frame.size.height);
self.searchBar.frame = CGRectMake(0, 0, self.view.bounds.size.width, kNaviBarHeight);
}];
}
// 隐藏搜索&城市列表
- (void)hideCityListView
{
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.searchBar resignFirstResponder];
self.searchBar.currentSearchKeywords = nil;
[UIView animateWithDuration:0.3 animations:^{
self.listContainerView.frame = CGRectMake(kTableViewMargin, CGRectGetMaxY(self.view.bounds), self.listContainerView.frame.size.width, self.listContainerView.frame.size.height);
self.searchBar.frame = CGRectMake(0, -kNaviBarHeight, self.view.bounds.size.width, kNaviBarHeight);
}];
}
//选择城市
- (void)cityListView:(MyCityListView *)listView didCitySelected:(MyCity *)city
{
MyCity *oldCity = [MyCityManager sharedInstance].currentCity;
//单独改变当前城市
if (!self.searchBar.doubleSearchModeEnable) {
//单独改变城市时修改当前城市
[self updateCurrentCity:city];
[self hideCityListView];
// 城市改变后清空
if (![oldCity.name isEqualToString:city.name]) {
self.locationView.endPOI = nil;
self.locationView.startPOI = nil;
// remove
[self.mapView removeAnnotation:self.startAnnotation];
[self.mapView removeAnnotation:self.endAnnotation];
}
//如果当前城市是定位城市直接进行当前定位的逆地理,否则进行地理编码获取城市位置。
if ([city.name isEqualToString:[MyCityManager sharedInstance].locationCity.name]) {
[self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES];
[self searchReGeocodeWithLocation:[AMapGeoPoint locationWithLatitude:self.mapView.userLocation.location.coordinate.latitude longitude:self.mapView.userLocation.location.coordinate.longitude]];
}
else {
[self searchGeocodeWithName:city.name];
}
}
else {
if (![oldCity.name isEqualToString:city.name]) {
self.searchBar.seachCity = city; // 只修改搜索city
[self updateSearchResultForCurrentCity];
}
}
}
func showCityListViewOnlyCity(_ onlyCity: Bool) {
navigationController?.setNavigationBarHidden(true, animated: true)
cityListView.reset()
searchResultView.poiArray = nil
searchBar.doubleSearchModeEnable = !onlyCity
searchBar.seachCity = MyCityManager.sharedInstance().currentCity
searchResultView.isHidden = onlyCity
if !onlyCity {
updateSearchResultForCurrentCity()
}
searchBar.reset()
searchBar.becomeFirstResponder()
UIView.animate(withDuration: 0.3, animations: {() -> Void in
self.listContainerView.frame = CGRect(x: CGFloat(kTableViewMargin), y: CGFloat(kTableViewMargin + kNaviBarHeight), width: CGFloat(self.listContainerView.frame.size.width), height: CGFloat(self.listContainerView.frame.size.height))
self.searchBar.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(self.view.bounds.size.width), height: CGFloat(kNaviBarHeight))
})
}
func hideCityListView() {
navigationController?.setNavigationBarHidden(false, animated: true)
searchBar.resignFirstResponder()
searchBar.currentSearchKeywords = nil
UIView.animate(withDuration: 0.3, animations: {() -> Void in
self.listContainerView.frame = CGRect(x: CGFloat(kTableViewMargin), y: CGFloat(self.view.bounds.maxY), width: CGFloat(self.listContainerView.frame.size.width), height: CGFloat(self.listContainerView.frame.size.height))
self.searchBar.frame = CGRect(x: CGFloat(0), y: CGFloat(-kNaviBarHeight), width: CGFloat(self.view.bounds.size.width), height: CGFloat(kNaviBarHeight))
})
}
func cityListView(_ listView: MyCityListView!, didCitySelected city: MyCity!) {
let oldCity: MyCity? = MyCityManager.sharedInstance().currentCity
//单独改变当前城市
if !searchBar.doubleSearchModeEnable {
//单独改变城市时修改当前城市
updateCurrentCity(city)
hideCityListView()
// 城市改变后清空
if !(oldCity?.name == city.name) {
locationView.endPOI = nil
locationView.startPOI = nil
// remove
mapView.removeAnnotation(startAnnotation)
mapView.removeAnnotation(endAnnotation)
}
//如果当前城市是定位城市直接进行当前定位的逆地理,否则进行地理编码获取城市位置。
if (city.name == MyCityManager.sharedInstance().locationCity.name) {
mapView.setCenter(mapView.userLocation.location.coordinate, animated: true)
searchReGeocode(withLocation: AMapGeoPoint.location(withLatitude: CGFloat(mapView.userLocation.location.coordinate.latitude), longitude: CGFloat(mapView.userLocation.location.coordinate.longitude)))
}
if (city.name == MyCityManager.sharedInstance().locationCity.name) {
mapView.setCenter(mapView.userLocation.location.coordinate, animated: true)
searchReGeocode(withLocation: AMapGeoPoint.location(withLatitude: CGFloat(mapView.userLocation.location.coordinate.latitude), longitude: CGFloat(mapView.userLocation.location.coordinate.longitude)))
}
else {
searchGeocode(withName: city.name)
}
}
else {
if !(oldCity?.name == city.name) {
searchBar.seachCity = city
// 只修改搜索city
updateSearchResultForCurrentCity()
}
}
}
该示例主要展示出行类app乘客端用户在设置起点和终点的方案。
类 | 接口 | 说明 | 版本 |
---|---|---|---|
AMap | - | 显示地图 | V2.0.0版本起 |
PoiSearch | searchPOIAsyn() | 查询POI异步接口 | V2.1.0版本起 |
PoiSearch | setOnPoiSearchListener(PoiSearch.OnPoiSearchListener listener) | 设置查询监听接口 | V2.1.0版本起 |
核心类/接口
类 | 接口 | 说明 | 版本 |
---|---|---|---|
AMapLocationClientOption | setOnceLocation(Boolean b); | 设置单次定位接口 | V2.0.0版本起 |
AMapLocationClientOption | setInterval(long time); | 设置连续定位时间间隔 | V2.0.0版本起 |
AMapLocationClient | startLocation(); | 启动定位 | V2.0.0版本起 |
AMapLocationClient | setLocationOption(mLocationOption); | 给定位客户端设置参数 | V2.0.0版本起 |
AMapLocationListener | onLocationChanged(AMapLocation amapLocation); | 监听器回调方法 | V2.0.0版本起 |
1、启动时通过定位SDK获取当前位置
采用定位SDK提供的高精度定位功能,定位前打开设备的wifi模块提升定位精度。
2、通过搜索获取位置
可参考示例中向高德POI搜索接口传参数的方式和内容。