有没有什么方法可以用GPS坐标检测地点? (尽可能不地图)例如,如果检查的经度和纬度是等于或接近这些:iOS - 用GPS坐标检测地点
44 35′25″n 104 42′55″w
or
44.5903 -104.7153
然后警报弹出并显示一条消息! 谢谢。
有没有什么方法可以用GPS坐标检测地点? (尽可能不地图)例如,如果检查的经度和纬度是等于或接近这些:iOS - 用GPS坐标检测地点
44 35′25″n 104 42′55″w
or
44.5903 -104.7153
然后警报弹出并显示一条消息! 谢谢。
你可以衡量你的 “硬编码” 的位置和当前用户位置之间的距离有以下几点:
CLLocation *hardcoded_location = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1];
CLLocation *new_location = newLocation;
CLLocationDistance distance = [hardcoded_location distanceFromLocation:new_location];
然后,你可以这样做:
if (distance < some_value)
{
[[[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
UPDATE:
typedef double CLLocationDistance
:
的距离测量(以米为单位)从现有的位置。
此代码将被用于解决您的问题。
CLLocation *currentLocation = newLocation;
if (currentLocation != nil)
NSLog(@"longitude = %.8f\nlatitude = %.8f", currentLocation.coordinate.longitude,currentLocation.coordinate.latitude);
// stop updating location in order to save battery power
[locationManager stopUpdatingLocation];
// Reverse Geocoding
NSLog(@"Resolving the Address");
// “reverseGeocodeLocation” method to translate the locate data into a human-readable address.
// The reason for using "completionHandler" ----
// Instead of using delegate to provide feedback, the CLGeocoder uses “block” to deal with the response. By using block, you do not need to write a separate method. Just provide the code inline to execute after the geocoding call completes.
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0)
{
placemark = [placemarks lastObject];
// strAdd -> take bydefault value nil
NSString *strAdd = nil;
if ([placemark.subThoroughfare length] != 0)
strAdd = placemark.subThoroughfare;
if ([placemark.thoroughfare length] != 0)
{
// strAdd -> store value of current location
if ([strAdd length] != 0)
strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark thoroughfare]];
else
{
// strAdd -> store only this value,which is not null
strAdd = placemark.thoroughfare;
}
}
if ([placemark.postalCode length] != 0)
{
if ([strAdd length] != 0)
strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark postalCode]];
else
strAdd = placemark.postalCode;
}
if ([placemark.locality length] != 0)
{
if ([strAdd length] != 0)
strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark locality]];
else
strAdd = placemark.locality;
}
if ([placemark.administrativeArea length] != 0)
{
if ([strAdd length] != 0)
strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark administrativeArea]];
else
strAdd = placemark.administrativeArea;
}
if ([placemark.country length] != 0)
{
if ([strAdd length] != 0)
strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark country]];
else
strAdd = placemark.country;
}
是的,你可以检测到设备何时接近某个特定位置(lat,lng)但你必须提一下接近。你不能用lat long值做简单的普通加法和减法。你必须指定一个半径或窗口。
distanceFromCurrentLocation = [userLocation distanceFromLocation:destinationlocation]/convertToKiloMeter;
if(distanceFromCurrentLocation < yourHigherBound && distanceFromLocation > yourLowerBound)
{
NSLog(@"yes, this place is inside my circle");
//Do the geocoding mentioned by others after this to get place name or country or whatever
}
else
{
NSLog(@"Oops!! its too far");
}
使用此方法
@interface YourClass : UIViewController
{
CLGeocoder *geocoder;
}
@end
-(void)viewDidLoad
{
geocoder = [[CLGeocoder alloc] init];
CLLocationCoordinate2D location;
location.latitude = 44.5903f;
location.longitude = -104.7153f;
[self startGeocoderWithLocation:location];
}
-(void)startGeocoderWithLocation:(CLLocationCoordinate2D)location
{
CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:location.latitude longitude:location.longitude];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
if (error == nil && [placemarks count] > 0)
{
CLPlacemark *placemark = [placemarks lastObject];
NSLog(@"%@",placemark.addressDictionary);
// you can use placemark.thoroughfare, placemark.locality, etc
}
else
{
//Error in finding location
}
} ];
}
公司希望它可以帮助
+1 distanceFromLocation! – satheeshwaran
好的答案,只是一个问题,如果提到'some_value'作为米我应该刚刚提到1000CM值为1米?或应该计算它? –
@ Mc.Lover 1m是100cm不是1000,总是以米计算 – AlexWien