CLLocation of distanceFromLocationマップ上の距離を計算するメソッド(要約)

Cllocation Distancefromlocation Method Calculating Distance Map

たとえば、ある場所までの距離とユーザーの現在の場所を計算するために、2つのオブジェクトCLLocationを準備します。
これはCLLocationuserLocation = [locationManager location]にあり、locationManagerはCLLocationManagerの例です。
そして[locationManagerstartUpdatingLocation]を実行しました
そして、2つのCLLocationの距離を計算します( 12.34 km )。
[NSString stringWithFormat:@ '%0.2f km'、[userLocationdistanceFromLocation:ロケーション]/ 1000]

2つの緯度と経度の間の距離を計算するためのObcective-Cコード

CLLocation*newLocation =[[CLLocation alloc] initWithCoordinate: newCoordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil]

CLLocationDistance kilometers =[newLocation distanceFromLocation:oldLocation]/1000


convertPoint:toCoordinateFromView:
CLLocationDistance dist =[loc1 distanceFromLocation:loc2]

距離は、座標間ではなく、2つのCLLocations間で計算されます。



次のコード行を使用してそれぞれの座標のCLLocationsを取得するには、これらの座標を使用する必要があります

answer:

他の座標についても同様に、次のコード行を使用して、これら2つの場所の間の距離を計算できます。

// You first have to get the corner point and convert it to a coordinate
MKMapRect mapRect = self.mapView.visibleMapRect
MKMapPoint cornerPointNW =MKMapPointMake(mapRect.origin.x, mapRect.origin.y)
CLLocationCoordinate2D cornerCoordinate =MKCoordinateForMapPoint(cornerPointNW)

// Then get the center coordinate of the mapView (just a shortcut for convenience)
CLLocationCoordinate2D centerCoordinate = self.mapView.centerCoordinate

// And then calculate the distance
CLLocationDistance distance =[cornerCoordinate distanceFromLocation:centerCoordinate]


ask:

尋ねる:

こんにちはiPhoneアプリで画像に示されているようにMKMapViewで2点間の距離を計算する方法

最初のポイントは、mapviewで表示されるマップの中心点になります

2番目のポイントは、mapviewでマップビューの表示されている長方形のいずれかのコーナーになります(ここでは、たとえば、左上のポイントを使用しています)

回答:

あなたは次の方法で中心の緯度/経度を取得できます:

-(void)locationManager:(CLLocationManager*)manager
didUpdateToLocation
:(CLLocation*)newLocation
fromLocation
:(CLLocation*)oldLocation
{
validLocation
= YES

if(!newLocation)
{
validLocation
= NO
}

if(newLocation.horizontalAccuracy <0)
{
validLocation
= NO
}

// Filter out points that are out of order
NSTimeInterval secondsSinceLastPoint =-[newLocation.timestamp timeIntervalSinceNow]
if(secondsSinceLastPoint <0)
{
validLocation
= NO
}

if(validLocation == YES)
{
[self.delegate locationChange:newLocation :oldLocation]
}

loc1とloc2はどちらもCLLocationオブジェクトです。

-(void)locationChange:(CLLocation*)newLocation:(CLLocation*)oldLocation
{

CLLocationDistance meters =[newLocation distanceFromLocation:oldLocation]

currentSpeed
=([newLocation speed]*3600)/1000
totalDistance
=(totalDistance + meters)/1000
totalDistanceMeters
= totalDistanceMeters + meters
avgSpeed
= totalDistance / counterInt

[speedLbl1 setText:[NSString stringWithFormat:@'%.3f', currentSpeed]]
[distanceLbl1 setText:[NSString stringWithFormat:@'%.3f', totalDistance]]

したがって、これらの2つのヒントが役立つはずです。コードが必要な場合はお知らせください:-)


必要な距離を計算する方法は次のとおりです。

totalDistance = totalDistance +(meters /1000)
totalDistance =(totalDistance + meters)/1000
totalDistance =(0+10)/1000=0.01
totalDistance
=(0.01+10)/1000=0.01001//!
totalDistance
=(0.01001+10)/1000=0.01001001//!


総移動距離を計算してViewControllerに出力しようとしましたが、期待どおりの結果が得られませんでした。コードは次のとおりです。

MyCLController.m

According Obcective-C code for calculation of the distance between two points of latitude and longitude, carried a CocoaChina members blog ( Link ), 
Wherein where er is the radius of the earth ellipsoid, google map for use 6378137 it. Call the function is very simple, suitable for almost any platform :)

NewWorkoutViewController.m

#define PI 3.1415926 
double LantitudeLongitudeDist(double lon1,double lat1,
double lon2,double lat2)
{
double er = 6378137 // 6378700.0f
//ave. radius = 6371.315 (someone said more accurate is 6366.707)
//equatorial radius = 6378.388
//nautical mile = 1.15078
double radlat1 = PI*lat1/180.0f
double radlat2 = PI*lat2/180.0f
//now long.
double radlong1 = PI*lon1/180.0f
double radlong2 = PI*lon2/180.0f
if( radlat1 <0 ) radlat1 = PI/2 + fabs(radlat1)// south
if( radlat1 > 0 ) radlat1 = PI/2 - fabs(radlat1)// north
if( radlong1 <0 ) radlong1 = PI*2 - fabs(radlong1)//west
if( radlat2 <0 ) radlat2 = PI/2 + fabs(radlat2)// south
if( radlat2 > 0 ) radlat2 = PI/2 - fabs(radlat2)// north
if( radlong2 <0 ) radlong2 = PI*2 - fabs(radlong2)// west
//spherical coordinates x=r*cos(ag)sin(at), y=r*sin(ag)*sin(at), z=r*cos(at)
//zero ag is up so reverse lat
double x1 = er * cos(radlong1) * sin(radlat1)
double y1 = er * sin(radlong1) * sin(radlat1)
double z1 = er * cos(radlat1)
double x2 = er * cos(radlong2) * sin(radlat2)
double y2 = er * sin(radlong2) * sin(radlat2)
double z2 = er * cos(radlat2)
double d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2))
//side, side, side, law of cosines and arccos
double theta = acos((er*er+er*er-d*d)/(2*er*er))
double dist = theta*er
return dist
}

}

問題は私のtotalDistanceにあり、毎回追加されているようには見えません。車でテストすると、座標間の値が10/20メートルであることがわかります。これは、distanceFromLocationを示しています。動作しているようです。

誰か考えはありますか?

 
1: Get the current latitude and longitude of the phone 
@implementation CurrentLocation
@synthesize locationManager
@synthesize target,callBack

#pragma mark --
#pragma mark Public

-(void) startUpdatingLocation{

[[self locationManager] startUpdatingLocation]

}

#pragma mark --
#pragma mark Memory management

-(void) dealloc{

[super dealloc]

[locationManager release]

}

#pragma mark --
#pragma mark Location manager

/*

Return a location manager -- create one if necessary.

*/

- (CLLocationManager *)locationManager {

if (locationManager != nil) {return locationManager}

locationManager = [[CLLocationManager alloc] init]

[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]

[locationManager setDelegate:self]

return locationManager

}

#pragma mark --
#pragma mark CLLocationManagerDelegate methods

/*

Conditionally enable the Add button:

If the location manager is generating updates, then enable the button

If the location manager is failing, then disable the button.

*/

- (void)locationManager:(CLLocationManager *)manager

didUpdateToLocation:(CLLocation *)newLocation

fromLocation:(CLLocation *)oldLocation {

NSLog(@'To get latitude and longitude!')

}

- (void)locationManager:(CLLocationManager *)manager

didFailWithError:(NSError *)error {

NSLog(@'Failed to get!')

}

@end


2: Get the current latitude and longitude of the phone Address


@implementation AddressReverseGeoder

#pragma mark --
#pragma mark Public

これを試して :

//Start address for more information based on latitude and longitude 
- (void)startedReverseGeoderWithLatitude:(double)latitude longitude:(double)longitude{

CLLocationCoordinate2D coordinate2D

coordinate2D.longitude = longitude

coordinate2D.latitude = latitude

//

MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate2D]

geoCoder.delegate = self

[geoCoder start]

}

#pragma mark --
#pragma mark MKReverseGeocoderDelegate methods

//Obtain address information

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {

NSString *address = [NSString stringWithFormat:@'%@ %@ %@ %@ %@%@',

placemark.country,

placemark.administrativeArea,

placemark.locality,

placemark.subLocality,

placemark.thoroughfare,

placemark.subThoroughfare]

NSLog(@'The latitude and longitude corresponding details:% @', address)

geocoder = nil

}

//Error Handling

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {

NSLog(@'error %@' , error)

}

#pragma mark --
#pragma mark Memory management

- (void)dealloc {

[super dealloc]

}

@end

これの代わりに

 

あなたがそれを持っていた方法では、totalDistanceは毎回1000で除算されていました。つまり、毎回10m移動している場合:

 
|_+_|
|_+_|

複製:https://www.cnblogs.com/pengyingh/articles/2373246.html