/****************************************************************************
Copyright (c) 2010-2013 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#import "AppController.h"
#import "cocos2d.h"
#import "AppDelegate.h"
#import "RootViewController.h"
#import "SDKWrapper.h"
#import "platform/ios/CCEAGLView-ios.h"
#import "MessageViewController.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#define APP_NAME @"hs_jindian13s"
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
static void SendToJS(const char* str){
se::ScriptEngine::getInstance()->evalString(str);
}
using namespace cocos2d;
@interface AppController ()<AMapLocationManagerDelegate>
@end
@implementation AppController
Application* app = nullptr;
static float latitude = -1;
static float longgitude = -1;
static NSString *city = @"";
@synthesize window;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[SDKWrapper getInstance] application:application didFinishLaunchingWithOptions:launchOptions];
// Add the view controller's view to the window and display.
float scale = [[UIScreen mainScreen] scale];
CGRect bounds = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame: bounds];
// cocos2d application instance
app = new AppDelegate(bounds.size.width * scale, bounds.size.height * scale);
app->setMultitouch(true);
// Use RootViewController to manage CCEAGLView
_viewController = [[RootViewController alloc]init];
#ifdef NSFoundationVersionNumber_iOS_7_0
_viewController.automaticallyAdjustsScrollViewInsets = NO;
_viewController.extendedLayoutIncludesOpaqueBars = NO;
_viewController.edgesForExtendedLayout = UIRectEdgeAll;
#else
_viewController.wantsFullScreenLayout = YES;
#endif
MessageViewController* Voiceview = [[MessageViewController alloc] init];
[_viewController.view addSubview:Voiceview.view];
// Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0){
// warning: addSubView doesn't work on iOS6
[window addSubview: _viewController.view];
}else{
// use this method on ios6
[window setRootViewController:_viewController];
}
[window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
//run the cocos2d-x game scene
app->start();
[AMapServices sharedServices].apiKey =@"高德key";
[self configLocationManager];
[self startLocation];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
[[SDKWrapper getInstance] applicationWillResignActive:application];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
[[SDKWrapper getInstance] applicationDidBecomeActive:application];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
[[SDKWrapper getInstance] applicationDidEnterBackground:application];
app->applicationDidEnterBackground();
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
[[SDKWrapper getInstance] applicationWillEnterForeground:application];
app->applicationWillEnterForeground();
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[[SDKWrapper getInstance] applicationWillTerminate:application];
delete app;
app = nil;
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
#pragma location
- (void)configLocationManager {
self.locationManager = [[AMapLocationManager alloc] init];
[self.locationManager setDelegate:self];
//設定不允許系統暫停定位
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
//設定允許在背景定位
[self.locationManager setAllowsBackgroundLocationUpdates:NO];
//設定允許連續定位逆地理
[self.locationManager setLocatingWithReGeocode:YES];
//設定定位最小更新距離方法如下,機關米。當兩次定位距離滿足設定的最小更新距離時,SDK會傳回符合要求的定位結果
[self.locationManager setDistanceFilter:200];
}
- (void) startLocation {
[self.locationManager startUpdatingLocation];
}
- (void)cleanUpAction {
//停止定位
[self.locationManager stopUpdatingLocation];
[self.locationManager setDelegate:nil];
}
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode {
// NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
latitude = location.coordinate.latitude;
longgitude = location.coordinate.longitude;
city = reGeocode.city;
NSLog(@"lat:%f;lon:%f; city : %@", latitude, longgitude, city);
}
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
}
//緯度
+ (float) getLatitude {
return latitude;
}
//經度
+ (float) getLonggitude {
return longgitude;
}
//市
+ (NSString*) getCity {
return city;
}
@end
前端js
sendLocation() {
let Longgitude= ''; //經度
let Latitude= ''; //緯度
if (cc.sys.OS_ANDROID === cc.sys.os) {
Latitude= jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getLatitude", "()Ljava/lang/String;");
Longgitude= jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getLongitude", "()Ljava/lang/String;");
} else if (cc.sys.OS_IOS === cc.sys.os) {
Latitude = jsb.reflection.callStaticMethod("AppController", "getLatitude");
Longgitude= jsb.reflection.callStaticMethod("AppController", "getLonggitude");
}
},