这个通常在数据库就已经把每个店的地标latitude 和longitude保存起来,然后用手机提取用户当前的location,与这些点的location比较,范围自己定。
至于比较是在服务器端做呢还是在iPhone上做呢,这个就要程序员设计,通常数据量大就在服务器做比较然后返回结果在iphone上直接显示就是了。
如果在iPhone上做比较就看我下面的代码
CLLocation *shopLocation=[[CLLocation alloc] initWithLatitude:[[location objectForKey:@"Latitude"] doubleValue]
longitude:[[location objectForKey:@"Longitude"] doubleValue]];
CLLocation *myLocation=[[CLLocation alloc] initWithLatitude:myLocationManager.location.coordinate.latitude
longitude:myLocationManager.location.coordinate.longitude];//在手机上测试
//CLLocation *myLocation=[[CLLocation alloc] initWithLatitude:-27.44 longitude:153.00];//在simulator上测试
double distance = [shopLocation getDistanceFrom:myLocation]/1000;
NSString *distanceChange=[NSString stringWithFormat:@"%g",distance];
NSLog(@"distance:%@ km",distanceChange);
我是把店的location放在一个Dictionary里面用来初始化shopLocation,distance = [shopLocation getDistanceFrom:myLocation]/1000这句就是用店的跟用户的比较
至于比较是在服务器端做呢还是在iPhone上做呢,这个就要程序员设计,通常数据量大就在服务器做比较然后返回结果在iphone上直接显示就是了。
如果在iPhone上做比较就看我下面的代码
CLLocation *shopLocation=[[CLLocation alloc] initWithLatitude:[[location objectForKey:@"Latitude"] doubleValue]
longitude:[[location objectForKey:@"Longitude"] doubleValue]];
CLLocation *myLocation=[[CLLocation alloc] initWithLatitude:myLocationManager.location.coordinate.latitude
longitude:myLocationManager.location.coordinate.longitude];//在手机上测试
//CLLocation *myLocation=[[CLLocation alloc] initWithLatitude:-27.44 longitude:153.00];//在simulator上测试
double distance = [shopLocation getDistanceFrom:myLocation]/1000;
NSString *distanceChange=[NSString stringWithFormat:@"%g",distance];
NSLog(@"distance:%@ km",distanceChange);
我是把店的location放在一个Dictionary里面用来初始化shopLocation,distance = [shopLocation getDistanceFrom:myLocation]/1000这句就是用店的跟用户的比较
然后判断这个dictance是否超过你的距离,入if(dictance>5){}
转载自:http://www.cocoachina.com/bbs/read.php?tid-70476.html
但是我发现 爱帮的API http://www.aibang.com/api/usage#locate 很是给力啊 哈哈