GPS distance calculator

i create a program. it is used to calculate the distance when the GPS is changing
but the program doesn’t work , it always crash and have some problem
problem one . how to keep give the update (longitude ,latitude)
problem two. in the program the total distance always 12xxxxx meters (extremely large)
hope someone can give me some suggestion

     using System;
    using UnityEngine;  
    using UnityEngine.UI;
   using System.Collections; 
      using UnityEngine.SceneManagement;

       public class run : MonoBehaviour {  
   public Text  lonAText, latAText, lonBText, latBText;
bool firstTime= true;

public Text result, overallResult, speedText, lastTimeText, timerText, accelerationText, speed0Text;

float  getA, getB,lonA, lonB, latA, latB, lastDistance;
double overalldistance=0 ;
public string gps_info = "";  
public int flash_num = 1;  

IEnumerator StartGPS () {  
	// Input.location 用于访问设备的位置属性(手持设备), 静态的LocationService位置  
	// LocationService.isEnabledByUser 用户设置里的定位服务是否启用 
	Input.location.Start ();
	if (!Input.location.isEnabledByUser) {  
		this.gps_info = "isEnabledByUser value is:"+Input.location.isEnabledByUser.ToString()+" Please turn on the GPS";   
		yield break;   
	}  

	// LocationService.Start() 启动位置服务的更新,最后一个位置坐标会被使用  
	Input.location.Start(10.0f, 10.0f);  

	int maxWait = 20;  
	while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {  
		// 暂停协同程序的执行(1秒)  
		yield return new WaitForSeconds(1);  
		maxWait--;  
	}  

	if (maxWait < 1) {  
		this.gps_info = "Init GPS service time out";  
		yield return false;    
	}  

	if (Input.location.status == LocationServiceStatus.Failed) {  
		this.gps_info = "Unable to determine device location";  
		yield return false;   
	}   
	else {  
		this.gps_info = "N:" + Input.location.lastData.latitude + " E:"+Input.location.lastData.longitude;  
		this.gps_info = this.gps_info + " Time:" + Input.location.lastData.timestamp;
		lonA = Input.location.lastData.longitude;
		latA = Input.location.lastData.latitude;
		//yield return new WaitForSeconds(1);  
		 
	}  
} 
// Use this for initialization  
void Start () {  
	
}  

void OnGUI () {  
	GUI.skin.label.fontSize = 28;  
	GUI.Label(new Rect(20,20,600,48),this.gps_info);   
	GUI.Label(new Rect(20,50,600,48),this.flash_num.ToString());
	GUI.Label(new Rect(20,80,600,48),"total distance:"+overalldistance);

	GUI.skin.button.fontSize = 50;  
	if (GUI.Button(new Rect(Screen.width/2-110,200,220,85),"GPS定位"))  
	{  
		// 这里需要启动一个协同程序 

	
		StartCoroutine(StartGPS()); 

	}  
	if (GUI.Button(new Rect(Screen.width/2-110,100,220,85),"刷新GPS"))  
	{  
		this.gps_info = "N:" + Input.location.lastData.latitude + " E:"+Input.location.lastData.longitude;  
		this.gps_info = this.gps_info + " Time:" + Input.location.lastData.timestamp;  
		this.flash_num += 1;   
	}  
		
}
// Input.location = LocationService  
// LocationService.lastData = LocationInfo  
	void Update()
		{ 
	
	getA = Input.location.lastData.longitude;
	getB = Input.location.lastData.latitude;
	 
	if (Input.location.lastData.latitude!=0 &&Input.location.lastData.longitude!=0) {

		if (lonA != Input.location.lastData.longitude || latA != Input.location.lastData.latitude) {

			var distance = CalculateDistancesM (latA, lonA, Input.location.lastData.latitude, Input.location.lastData.longitude);  // last distance and overall distanceS            
			lonA = Input.location.lastData.longitude;
			latA = Input.location.lastData.latitude;

			overalldistance = overalldistance + distance;

		} 
	}

	
}   

void StopGPS () {  
	//Input.location.Stop();  
}  
double _eQuatorialEarthRadius = 6378.1370D;
double _d2r = (Math.PI / 180D);

private int CalculateDistancesM(double lat1, double long1, double lat2, double long2)
{
	return (int)(1000D * HaversineInKM(lat1, long1, lat2, long2));
}

private double HaversineInKM(double lat1, double long1, double lat2, double long2)
{
	double dlong = (long2 - long1) * _d2r;
	double dlat = (lat2 - lat1) * _d2r;
	double a = Math.Pow(Math.Sin(dlat / 2D), 2D) + Math.Cos(lat1 * _d2r) * Math.Cos(lat2 * _d2r) * Math.Pow(Math.Sin(dlong / 2D), 2D);
	double c = 2D * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1D - a));
	double d = _eQuatorialEarthRadius * c;

	return d;
}

}

http://wirebeings.com/markerless-gps-ar.html
try this ,this may help you