媒体广告变现优化之道(七)

媒体广告变现优化之道(七)

今天我们研究下如何获取地理位置信息。OpenRTB 中有一个geo字段,该字段对应一个geo对象,这个对象中有好多字段,这里只讨论两个关键的字段lat和lon字段的获取。其中lat表示纬度,lon表示经度。
获取 Android 设备的地理位置信息importandroid.Manifest;importandroid.content.Context;importandroid.content.pm.PackageManager;importandroid.location.Location;importandroid.location.LocationListener;importandroid.location.LocationManager;importandroid.os.Bundle;importandroid.os.Process;importjava.util.List;finalclassGeoInfo{privatestaticLocationListenerlocationListener;privatestaticvolatileLocationlocation;staticvoidinit(Contextcontext){finalLocationManagerlocationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);ListStringproviders=locationManager.getProviders(true);Stringprovider=null;if(context.checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION,Process.myPid(),Process.myUid())==PackageManager.PERMISSION_GRANTED){if(providers.contains(LocationManager.NETWORK_PROVIDER)){provider=LocationManager.NETWORK_PROVIDER;}}if(provider==null){if(context.checkPermission(Manifest.permission.ACCESS_FINE_LOCATION,Process.myPid(),Process.myUid())==PackageManager.PERMISSION_GRANTED){if(providers.contains(LocationManager.GPS_PROVIDER)){provider=LocationManager.GPS_PROVIDER;}elseif(providers.contains(LocationManager.PASSIVE_PROVIDER)){provider=LocationManager.PASSIVE_PROVIDER;}}}if(provider==null){return;}location=locationManager.getLastKnownLocation(provider);if(location!=null){return;}locationListener=newLocationListener(){@OverridepublicvoidonLocationChanged(Locationlocation){GeoInfo.location=location;if(locationListener!=null){locationManager.removeUpdates(locationListener);}}@OverridepublicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){}@OverridepublicvoidonProviderEnabled(Stringprovider){}@OverridepublicvoidonProviderDisabled(Stringprovider){}finalStringfinalProvider=provider;if(context.checkPermission(Manifest.permission.ACCESS_FINE_LOCATION,Process.myPid(),Process.myUid())==PackageManager.PERMISSION_GRANTEDcontext.checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION,Process.myPid(),Process.myUid())==PackageManager.PERMISSION_GRANTED){try{locationManager.requestLocationUpdates(finalProvider,0,0,locationListener);}catch(Exceptione){}}}staticdoublegetLatitude(){if(location==null)return0.0;returnlocation.getLatitude();}staticdoublegetLongitude(){if(location==null)return0.0;returnlocation.getLongitude();}staticfloatgetAccuracy(){if(location==null)return0.0f;returnlocation.getAccuracy();}}这里为了不增加依赖,我们直接用了Context的checkPermission方法来进行权限检测。
另外,在AndroidManifest.xml中还需要加入下面的权限声明:
uses-permissionandroid:name=”android.permission.ACCESS_COARSE_LOCATION”/uses-permissionandroid:name=”android.permission.ACCESS_FINE_LOCATION”/获取 iOS 设备的地理位置信息#importFoundation/Foundation.h#importCoreLocation/CoreLocation.h@interfaceGeoInfo()CLLocationManagerDelegate@end@implementationGeoInfostaticconstGeoInfo*delegate;staticconstCLLocationManager*locationManager;staticdoublelatitude=0;staticdoublelongitude=0;staticdoubleaccuracy=0; (void)load{dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{delegate=[GeoInfonew];locationManager=[CLLocationManagernew];locationManager.delegate=delegate;locationManager.desiredAccuracy=kCLLocationAccuracyBest;[locationManagerrequestWhenInUseAuthorization];[locationManagerstartMonitoringSignificantLocationChanges];[locationManagerstartUpdatingLocation];}-(void)locationManager:(CLLocationManager*)managerdidUpdateLocations:(NSArrayCLLocation**)locations{CLLocation*location=[locationslastObject];latitude=location.coordinate.latitude;longitude=location.coordinate.longitude;accuracy=manager.desiredAccuracy;[locationManagerstopUpdatingLocation];} (constdouble)latitude{returnlatitude;} (constdouble)longitude{returnlongitude;} (constdouble)accuracy{returnaccuracy;}@end在 iOS 中要获取地理位置信息,除了使用上面的代码之外,还需要在info.plist添加Privacy – Location Always Usage Description或者Privacy – Location When In Use Usage Description权限,类型为String,value中一定要有值, 来告诉用户使用定位服务的目的(一直定位/当用户使用时定位)。然后在程序的Build Phases的Link Binary With Libraries导入CoreLocation.framework就可以了。

—END—相关阅读
媒体广告变现优化之道——设备信息获取(一)
媒体广告变现优化之道——设备信息获取(二)
媒体广告变现优化之道——设备信息获取(三)
媒体广告变现优化之道——设备信息获取(四)
媒体广告变现优化之道——设备信息获取(五)
媒体广告变现优化之道(六)——如何获取网络连接类型

▼更多精彩推荐,请关注我们▼

赞(0)
未经允许不得转载:第一SCI网 » 媒体广告变现优化之道(七)

评论 抢沙发

评论前必须登录!