C + + encapsulates a latitude and longitude class

Source: Internet
Author: User
Tags cos sin

Requirement: Obtain the surface distance between two points according to latitude and longitude, and calculate the direction between two points

Reference:

Android get latitude and longitude, calculate distance, azimuth angle there is a formula in the post, I will not repeat it. There is no function to write the azimuth, because it is very simple to use temporarily.

Calculate the distance and direction between the two latitude and longitude of the Earth, and the direction of calculation here is more specific.

This class will continue to be reconstructed in the future.

/* * JWD.h * *  Created on:2014 August 22 *      Author:zy */#ifndef jwd_h_#define jwd_h_#include<math.h>class jwd { PUBLIC:JWD (double longitude, double latitude), virtual ~jwd (),//radians double Radian (double D) {return d*pi/180.0;//angle 1? =π /180}double getdistance (jwd b); void Getdirection (JWD b);p rivate:double m_longitude; Longitude, dotted decimal form (Google Maps can get this data), longitude is positive, west longitude is negative double m_latitude; Latitude, north latitude is positive, south latitude is negative//constant The data member obtains the initial value through the initialization list, C++11 can initialize the const double Pi;const double Earth_radius to the non-static member constant; #endif/* Jwd_h_ */

/* * JWD.cpp * * Created on:2014 August 22 * author:zy */#include "JWD.h" #include <string> #include &LT;IOSTREAM&G T JWD::JWD (double longitude, double latitude):P I (3.1415927), Earth_radius (6378.137) {//TODO auto-generated constructor Stubm_longitude = Longitude;m_latitude = latitude;} JWD::~JWD () {//TODO auto-generated destructor stub}//return unit is M double jwd::getdistance (jwd B) {Double Radlatitudea = Radian ( This->m_latitude);d ouble radlatitudeb = Radian (b.m_latitude);d ouble Radlongitudea = Radian (this->m_longitude); Double radlongitudeb = Radian (b.m_longitude);d ouble Radlatitudediff = radlatitudea-radlatitudeb;double Radlongitudediff = radlongitudea-radlongitudeb;//Formula Double DST = 2 * ASIN ((sqrt (Pow (sin (RADLATITUDEDIFF/2), 2) + cos (RA Dlatitudea) * COS (radlatitudeb) * POW (sin (RADLONGITUDEDIFF/2), 2)));d st = DST * earth_radius;dst= round (DST * 10000000 )/10000;return DST;} What direction is the B point in a? void Jwd::getdirection (Jwd B) {//Note the number of digits obtained here, the constant pi to take a large point double ARad = atan2 (b.m_latitude-thiS->m_latitude,b.m_longitude-this->m_longitude) std::string str;std::cout << aRad << std::endl;if ( ARad >-pi/4 && (aRad <= Pi/4)) {str = "East";} else if (ARad > Pi/4 && (aRad <= PI*3/4)) {str = " North ";} else if ((ARad > Pi*3/4 && (aRad <= PI)) | | (ARad >=-pi && (aRad <=-PI*3/4))) {str = "West";} else if (ARad >=-pi*3/4 && (aRad <=-pi/4)) {str = "South";} Std::cout << str << Std::endl;} int main () {JWD A (116.41615, 39); JWD B (118, -40);d ouble distance = a.getdistance (b);std::cout<< distance << "M" <<std::endl; A.getdirection (B); B.getdirection (A); return 0;}


The output results are as follows:

8.79572e+06m-1.55075 South 1.59084 North


Description

1. Direction there, because I only need 4 directions to be able, do not need 8 directions, you can according to your needs to make changes;

2. The function has been tested, and the distance can be used to verify that the distance is calculated by latitude;

3. Google map to view latitude and longitude, open the Google Maps laboratory, which can be set;

4. Just getting Started C + +, where bad writing, please also put forward, thank you

C + + encapsulates a latitude and longitude class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.