Unity开发之插件---高德地图定位和地理围栏(iOS)
在前一篇记录了使用Unity编写高德定位及地理围栏的插件,下面介绍一下iOS。 复制一份高德的示例工程officialDemoLoc(省了配置工程,和引用库文件),创建AMapHelper类文件(.h和.mm),头文件主要包含AMapHelper单例类,和导出的几个方法,如下:
#import <Foundation/Foundation.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import "IUnityInterface.h"
#if defined(__cplusplus)
extern "C"{
#endif
extern void UnitySendMessage(const char *, const char *, const char *);
#if defined(__cplusplus)
}
#endif
extern "C" void UNITY\_INTERFACE\_EXPORT UNITY\_INTERFACE\_API MAMapUnityStartLocation(const char * apiKey);
extern "C" void UNITY\_INTERFACE\_EXPORT UNITY\_INTERFACE\_API MAMapUnityStopLocation();
extern "C" void UNITY\_INTERFACE\_EXPORT UNITY\_INTERFACE\_API MAMapUnityStartGeoFence(const char * jsonString);
extern "C" void UNITY\_INTERFACE\_EXPORT UNITY\_INTERFACE\_API MAMapUnityStopGeoFence();
extern "C" bool UNITY\_INTERFACE\_EXPORT UNITY\_INTERFACE\_API MAMapUnityContainsCoordinate(double lat,double lng);
@interface AMapHelper : NSObject<AMapLocationManagerDelegate,AMapGeoFenceManagerDelegate>
@property (nonatomic, strong) AMapLocationManager *locationManager;
//@property (nonatomic, strong) AMapGeoFenceManager *geoFenceManager;
@property (nonatomic, strong) AMapLocationRegion *locationRegion;
\+ (instancetype)sharedInstance;
@end
定位和围栏
\- (void)startLocation:(NSString *)apiKey{
\[\[AMapServices sharedServices\] setApiKey:apiKey\];
\[self configLocationManager\];
//开始进行连续定位
\[self.locationManager startUpdatingLocation\];
}
-(void)stopLocation{
//停止定位
\[self.locationManager stopUpdatingLocation\];
}
-(void)startGeoFence:(NSString*)jsonStr{
NSError *error = nil;
NSData *data = \[jsonStr dataUsingEncoding: NSUTF8StringEncoding\];
id jsonObject = \[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error\];
NSString *customId;
CLLocationCoordinate2D *coorArr;
if (\[jsonObject isKindOfClass:\[NSDictionary class\]\]) {
NSDictionary \*dict = (NSDictionary\*)jsonObject;
customId = \[dict valueForKey:@"customId"\];
NSArray *pointList =\[dict valueForKey:@"customId"\];
long count = \[pointList count\];
coorArr = (CLLocationCoordinate2D*)malloc(sizeof(CLLocationCoordinate2D) * count);
for(int i = 0;i<\[pointList count\];i++){
NSDictionary*item = \[pointList objectAtIndex:i\];
double lat = \[\[item objectForKey:@"latitude"\] doubleValue\];
double lng = \[\[item objectForKey:@"longitude"\] doubleValue\];
coorArr\[i\] = CLLocationCoordinate2DMake(lat, lng);
}
self.locationRegion = \[\[AMapLocationPolygonRegion alloc\] initWithCoordinates:coorArr count:count identifier:customId\];
//开始
\[self.locationManager startMonitoringForRegion:self.locationRegion\];
free(coorArr);
}
}
-(void)stopGeoFence{
\[self.locationManager stopMonitoringForRegion:_locationRegion\];
}
导出函数实现:
extern "C" void UNITY\_INTERFACE\_EXPORT UNITY\_INTERFACE\_API MAMapUnityStartLocation(const char * apiKey){
\[\[AMapHelper sharedInstance\] startLocation:\[NSString stringWithUTF8String:apiKey\]\];
}
extern "C" void UNITY\_INTERFACE\_EXPORT UNITY\_INTERFACE\_API MAMapUnityStopLocation(){
\[\[AMapHelper sharedInstance\] stopLocation\];
}
extern "C" void UNITY\_INTERFACE\_EXPORT UNITY\_INTERFACE\_API MAMapUnityStartGeoFence(const char * jsonString){
\[\[AMapHelper sharedInstance\] startGeoFence:\[NSString stringWithUTF8String:jsonString\]\];
}
extern "C" void UNITY\_INTERFACE\_EXPORT UNITY\_INTERFACE\_API MAMapUnityStopGeoFence(){
\[\[AMapHelper sharedInstance\] stopGeoFence\];
}
extern "C" bool UNITY\_INTERFACE\_EXPORT UNITY\_INTERFACE\_API MAMapUnityContainsCoordinate(double lat,double lng){
return \[\[AMapHelper sharedInstance\] containsCoordinate:lat lng:lng\];
}
将两个文件复制到Unity工程目录,保存在Plugins/iOS目录下,iOS可以在Plugins下任意目录,也可以有多个iOS目录。 Unity下调用iOS方法:
#elif UNITY_IOS
\[DllImport ("__Internal")\]
private static extern void MAMapUnityStartLocation (string apiKey);
\[DllImport ("__Internal")\]
private static extern void MAMapUnityStopLocation ();
\[DllImport ("__Internal")\]
private static extern void MAMapUnityStartGeoFence(string jsonString);
\[DllImport ("__Internal")\]
private static extern void MAMapUnityStopGeoFence ();
\[DllImport ("__Internal")\]
private static extern bool MAMapUnityContainsCoordinate(double lat,double lng);
public void StartLocation(string apiKey)
{
MAMapUnityStartLocation(apiKey);
}
public void StopLocation()
{
MAMapUnityStopLocation();
}
public void StartGeoFence(string jsonString)
{
MAMapUnityStartGeoFence(jsonString);
}
public void StopGeoFence()
{
MAMapUnityStopGeoFence();
}
#endif
使用方式和方法和Android相同。功能基本实现,性能及精度还有待优化。
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 xue_huashan@163.com
文章标题:Unity开发之插件---高德地图定位和地理围栏(iOS)
文章字数:621
本文作者:max-xue
发布时间:2018-01-03, 15:47:28
最后更新:2019-11-09, 21:47:08
原始链接:http://blog.le-more.com/2018/01/03/u3d/b0-e7-90-86-e5-9b-b4/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。