如题所述
用 isLocationOutOfChina 判断之前,你先把高德(火星)转成 WGS-84。
#import "TQLocationConverter.h"
#import <math.h>
+#import <UIKit/UIGeometry.h>
static const double a = 6378245.0;
static const double ee = 0.00669342162296594323;
@@ -131,12 +132,216 @@ static bool isContains(CLLocationCoordinate2D point, CLLocationCoordinate2D p1,
/**
* 判断是不是在中国
+ * 用引射线法判断 点是否在多边形内部
+ * 算法参考:http://www.cnblogs.com/luxiaoxun/p/3722358.html
*/
-+(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location
-{
- if (location.longitude < 72.004 || location.longitude > 137.8347 || location.latitude < 0.8293 || location.latitude > 55.8271)
- return YES;
- return NO;
++ (BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location {
+ CGPoint point = CGPointMake(location.latitude, location.longitude);
+ BOOL oddFlag = NO;
+ NSInteger j = [self polygonOfChina].count - 1;
+ for (NSInteger i = 0; i < [self polygonOfChina].count; i++) {
+ CGPoint polygonPointi = [[self polygonOfChina][i] CGPointValue];
+ CGPoint polygonPointj = [[self polygonOfChina][j] CGPointValue];
+ if (((polygonPointi.y < point.y && polygonPointj.y >= point.y) ||
+ (polygonPointj.y < point.y && polygonPointi.y >= point.y)) &&
+ (polygonPointi.x <= point.x || polygonPointj.x <= point.x)) {
+ oddFlag ^= (polygonPointi.x +
+ (point.y - polygonPointi.y) /
+ (polygonPointj.y - polygonPointi.y) *
+ (polygonPointj.x - polygonPointi.x) <
+ point.x);
+ }
+ j = i;
+ }
+ return !oddFlag;
+}
+
+// 中国大陆多边形,用于判断坐标是否在中国
+// 因为港澳台地区使用WGS坐标,所以多边形不包含港澳台地区
#import "TQLocationConverter.h"
#import <math.h>
+#import <UIKit/UIGeometry.h>
static const double a = 6378245.0;
static const double ee = 0.00669342162296594323;
@@ -131,12 +132,216 @@ static bool isContains(CLLocationCoordinate2D point, CLLocationCoordinate2D p1,
/**
* 判断是不是在中国
+ * 用引射线法判断 点是否在多边形内部
+ * 算法参考:http://www.cnblogs.com/luxiaoxun/p/3722358.html
*/
-+(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location
-{
- if (location.longitude < 72.004 || location.longitude > 137.8347 || location.latitude < 0.8293 || location.latitude > 55.8271)
- return YES;
- return NO;
++ (BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location {
+ CGPoint point = CGPointMake(location.latitude, location.longitude);
+ BOOL oddFlag = NO;
+ NSInteger j = [self polygonOfChina].count - 1;
+ for (NSInteger i = 0; i < [self polygonOfChina].count; i++) {
+ CGPoint polygonPointi = [[self polygonOfChina][i] CGPointValue];
+ CGPoint polygonPointj = [[self polygonOfChina][j] CGPointValue];
+ if (((polygonPointi.y < point.y && polygonPointj.y >= point.y) ||
+ (polygonPointj.y < point.y && polygonPointi.y >= point.y)) &&
+ (polygonPointi.x <= point.x || polygonPointj.x <= point.x)) {
+ oddFlag ^= (polygonPointi.x +
+ (point.y - polygonPointi.y) /
+ (polygonPointj.y - polygonPointi.y) *
+ (polygonPointj.x - polygonPointi.x) <
+ point.x);
+ }
+ j = i;
+ }
+ return !oddFlag;
+}
+
+// 中国大陆多边形,用于判断坐标是否在中国
+// 因为港澳台地区使用WGS坐标,所以多边形不包含港澳台地区
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-11-16
楼主好,开发问题请登录LBS创建工单咨询哦~