地理位置操作

添加地理位置

为地理位置 geojson 类型字段添加数据和为普通字段添加数据的操作方式是一致的,可参考以下示例。

参数说明

参数 类型 必填 说明
key String 在数据表中的类型必须是 geojson
value GeoPoint 或 GeoPolygon -

geojson 类型字段支持使用 GeoPointGeoPolygon 类型数据进行赋值:

  • GeoPoint 表示坐标点,经度 longitude 在前,纬度 latitude 在后,创建一个点:

{% tabs swift1=”Swift”, oc1=”Objective-C” %} {% content “swift1” %}

  1. let point = GeoPoint(longitude: 10, latitude: 10)

{% content “oc1” %}

  1. BaaSGEOPoint *point = [[BaaSGEOPoint alloc] initWithLongitude:10 latitude:10];

{% endtabs %}

  • GeoPolygon 表示地理形状,可以通过以下两种方法创建一个地理形状

info 创建一个地理形状时,第一个点和最后一个点必须重合,否则创建失败。

{% tabs swift2=”Swift”, oc2=”Objective-C” %} {% content “swift2” %}

  1. // 1. 直接使用数字
  2. let polygon = GeoPolygon(coordinates: [[10, 10], [20, 10], [30, 20], [10, 10]])
  3. // 2. 借助 GeoPoint
  4. let point1 = GeoPoint(longitude: 10, latitude: 10)
  5. let point2 = GeoPoint(longitude: 20, latitude: 10)
  6. let point3 = GeoPoint(longitude: 30, latitude: 20)
  7. let point4 = GeoPoint(longitude: 10, latitude: 10)
  8. let polygon = GeoPolygon(points: [point1, point2, point3, point4])

{% content “oc2” %}

  1. // 1. 直接使用数字
  2. BaaSGeoPolygon *polygon = [[BaaSGeoPolygon alloc] initWithCoordinates:@[@[@1, @1], @[@2, @2], @[@3, @3]];
  3. // 2. 借助 GeoPoint
  4. BaaSGEOPoint *point1 = [[BaaSGEOPoint alloc] initWithLongitude:10 latitude:10];
  5. BaaSGEOPoint *point2 = [[BaaSGEOPoint alloc] initWithLongitude:20 latitude:10];
  6. BaaSGEOPoint *point3 = [[BaaSGEOPoint alloc] initWithLongitude:30 latitude:20];
  7. BaaSGEOPoint *point4 = [[BaaSGEOPoint alloc] initWithLongitude:10 latitude:10];
  8. BAASGeoPolygon *polygon = [[BAASGeoPolygon alloc] initWithPoints:@[point1, point2, point3, point4];

{% endtabs %}

设置地理位置信息

表中有名称为 locationpolygon 的两列,类型都为 geojson

{% tabs swift2_1=”Swift”, oc2_1=”Objective-C” %} {% content “swift2_1” %}

  1. // Point 类型
  2. let point = GeoPoint(longitude: 2, latitude: 10)
  3. record.set("location", value: point)
  4. // GeoPolygon
  5. let polygon = GeoPolygon(coordinates: [[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]])
  6. record.set("polygon", value: polygon)

{% content “oc2_1” %}

  1. // Point类型
  2. BaaSGeoPoint *point = [[BaaSGeoPoint alloc] initWithLongitude:2 latitude:10];
  3. [record set:@"location" value:point];
  4. // GeoPolygon
  5. BaaSGeoPolygon *polygon = [[BaaSGeoPolygon alloc] initWithCoordinates:@[@[30, 10], @[40, 40], @[20, 40], @[10, 20], @[30, 10]]];
  6. [record set:@"polygon" value:polygon];

{% endtabs %}

地理位置查询

include 在指定多边形集合中找出包含某一点的多边形

{% tabs swift3=”Swift”, oc3=”Objective-C” %} {% content “swift3” %}

  1. // 查找当前用户所属小区
  2. let neighbourhood = Table(name: "neighbourhoodTableName")
  3. // geoField 为 neighbourhood 表中定义地理位置的字段名,point 为用户所在位置,为 GeoPoint 类型
  4. let whereArgs = Where.include("geoField", point: point)
  5. let query = Query()
  6. query.where = whereArgs
  7. neighbourhood.find(query: query, completion: {listResult, error in
  8. })

{% content “oc3” %}

  1. // 查找当前用户所属小区
  2. BaaSTable *neighbourhood = [[BaaSTable alloc] initWithName:@"neighbourhoodTableName"];
  3. // geoField 为 neighbourhood 表中定义地理位置的字段名,point 为用户所在位置,为 GeoPoint 类型
  4. BaaSWhere *where = [BaaSWhere include:"geoField" point:point];
  5. query.where = where;
  6. [neighbourhood findWithQuery:query completion:^(BaaSRecordList * _Nullable listResult, NSError * _Nullable error) {
  7. }];

{% endtabs %}

withinCircle 在指定点集合中,查找包含在指定圆心和指定半径所构成的圆形区域中的点,半径单位为 千米(km)。 (返回结果随机排序)

info radius 参数单位为 km。

{% tabs swift4=”Swift”, oc4=”Objective-C” %} {% content “swift4” %}

  1. // 查找在距离用户 radius 千米范围内的饭店
  2. let restaurant = Table(name: "restaurantTableName")
  3. // geoField 为 restaurant 表中定义地理位置的字段名
  4. let whereArgs = Where.withinCircle("geoField", point: point, radius: radius)
  5. let query = Query()
  6. query.where = whereArgs
  7. restaurant.find(query: query, completion: {listResult, error in
  8. })

{% content “oc4” %}

  1. // 查找在距离用户 radius 千米范围内的饭店
  2. BaaSTable *restaurant = [[BaaSTable alloc] initWithName:@"restaurantTableName"];
  3. // geoField 为 restaurant 表中定义地理位置的字段名
  4. BaaSWhere *where = [BaaSWhere withinCircle:@"geoField" point: point radius: radius];
  5. query.where = where;
  6. [restaurant findWithQuery:query completion:^(BaaSRecordList * _Nullable listResult, NSError * _Nullable error) {
  7. }];

{% endtabs %}

withinRegion 在指定点集合中,查找包含在以指定点为圆点,以最大和最小距离为半径,所构成的圆环区域中的点。半径单位为千米(km)。(返回结果按从近到远排序)

info maxDistance 与 minDistance 参数单位为 m。

{% tabs swift5=”Swift”, oc5=”Objective-C” %} {% content “swift5” %}

  1. // 查找距离用户 minDistance 米外,maxDistance 米内的所有饭店
  2. let restaurant = Table(name: "restaurantTableName")
  3. // geoField 为 restaurant 表中定义地理位置的字段名,point 为圆点,minDistance 不指定默认为 0
  4. let whereArgs = Where.withinRegion("geoField", point: point, minDistance: minDistance, maxDistance: maxDistance)
  5. let query = Query()
  6. query.where = whereArgs
  7. restaurant.find(query: query, completion: {listResult, error in
  8. })

{% content “oc5” %}

  1. // 查找距离用户 minDistance 米外,maxDistance 米内的所有饭店
  2. BaaSTable *restaurant = [[BaaSTable alloc] initWithName:@"restaurantTableName"];
  3. // geoField 为 restaurant 表中定义地理位置的字段名,point 为圆点,minDistance 不指定默认为 0
  4. BaaSWhere *where = [BaaSWhere withinRegion: point:point minDistance:minDistance maxDistance:maxDistance];
  5. query.where = where;
  6. [restaurant findWithQuery:query completion:^(BaaSRecordList * _Nullable listResult, NSError * _Nullable error) {
  7. }];

{% endtabs %}

within 在指定点集合中,查找包含于指定的多边形区域的点

{% tabs swift6=”Swift”, oc6=”Objective-C” %} {% content “swift6” %}

  1. // 查找某个小区内的所有饭店
  2. let restaurant = Table(name: "restaurantTableName")
  3. // neighbourhoodPolygon 表示某小区的地理位置
  4. let neighbourhoodPolygon: GeoPolygon
  5. // restaurantGeoField 为 restaurant 表中定义地理位置的字段名
  6. let whereArgs = Where.within("restaurantGeoField", polygon: neighbourhoodPolygon)
  7. let query = Query()
  8. query.where = whereArgs
  9. restaurant.find(query: query, completion: {listResult, error in
  10. })

{% content “oc6” %}

  1. // 查找某个小区内的所有饭店
  2. BaaSTable *restaurant = [[BaaSTable alloc] initWithName:@"restaurantTableName"];
  3. // neighbourhoodPolygon 表示某小区的地理位置
  4. BaaSGeoPolygon *neighbourhoodPolygon;
  5. // restaurantGeoField 为 restaurant 表中定义地理位置的字段名
  6. BaaSWhere *where = [BaaSWhere within: polygon:neighbourhoodPolygon];
  7. query.where = where;
  8. [restaurant findWithQuery:query completion:^(BaaSRecordList * _Nullable listResult, NSError * _Nullable error) {
  9. }];

{% endtabs %}

参数说明

参数 类型 必填 说明
query Query Y 查询条件,详见数据表 - 查询

返回结果

名称 类型 说明
listResult RecordList 结果列表,详见 数据类型 章节
error NSError 错误信息,参考错误处理和错误码