mapbox-unity-sdk icon indicating copy to clipboard operation
mapbox-unity-sdk copied to clipboard

Conversion.cs

Open luizjunior opened this issue 5 years ago • 5 comments

Hi Everyone!

I'm getting the following error when changing zoom factor during play:

ArgumentException: Wrong number of arguments Mapbox.Unity.Utilities.Conversions.StringToLatLon (System.String s) (at Assets/Mapbox/Unity/Utilities/Conversions.cs:49)

This is the function: public static Vector2d StringToLatLon(string s):44

When printing the s string: ArgumentException: Wrong number of arguments: -23,006977, -47,148561

I can see that for some reason, new LatLon values are being formatted for my country (Brazil) and not US format.

I think we can just fix this function to split coordinates by ", " (with spacing) and replacing "," with "." on the numbers:

public static Vector2d StringToLatLon(string s)
{
	var latLonSplit = s.Split(new string[] { ", " }, StringSplitOptions.None);
	if (latLonSplit.Length != 2)
	{
		throw new ArgumentException("Wrong number of arguments: " + s);
	}

	double latitude = 0;
	double longitude = 0;

	if (!double.TryParse(latLonSplit[0].Trim().Replace(",","."), NumberStyles.Any, NumberFormatInfo.InvariantInfo, out latitude))
	{
		throw new Exception(string.Format("Could not convert latitude to double: {0}", latLonSplit[0].Trim().Replace(",", ".")));
	}

	if (!double.TryParse(latLonSplit[1].Trim().Replace(",", "."), NumberStyles.Any, NumberFormatInfo.InvariantInfo, out longitude))
	{
		throw new Exception(string.Format("Could not convert longitude to double: {0}", latLonSplit[0].Trim().Replace(",", ".")));
	}

	return new Vector2d(latitude, longitude);
}

luizjunior avatar Jan 03 '20 18:01 luizjunior

Hello! It works! I had the same problem when I push any of moving keys on keyboard, it ruins the process with the same error. ArgumentException: Wrong number of arguments Mapbox.Unity.Utilities.Conversions.StringToLatLon (System.String s) (at Assets/Mapbox/Unity/Utilities/Conversions.cs:49). Tried luizjunior's decision, and it fixed the crash! Thank you!

Antohnio123 avatar Jan 11 '20 10:01 Antohnio123

@luizjunior I thought we use us-culture when we process those, are you using anything custom by any chance? Or is it happening only with the first tile (original latlng) you put in?

brnkhy avatar Jan 21 '20 13:01 brnkhy

Hi guys, sorry for the delay. I'm not using anything custom. My Window is in Portuguese/Brazil and the number format is 1.000,00 (dot for thousand and , for decimals)

This happens when formatting the new URL data request.

luizjunior avatar Jan 31 '20 17:01 luizjunior

I'm French and had the same issue, @luizjunior's solution works.

bouiboui avatar Jun 08 '20 13:06 bouiboui

It's not working anymore, because on certain example scenes coordinates are sent separated by only a comma, there's no space after anymore. So you can do this instead :

public static Vector2d StringToLatLon(string s)
{
  var latLonSplit = s.Split(new string[] { "," }, StringSplitOptions.None);
  // Case where the decimal separator is a comma
  if (latLonSplit.Length == 4) {
    latLonSplit[0] = string.Join(",", latLonSplit[0], latLonSplit[1]);
    latLonSplit[1] = string.Join(",", latLonSplit[2], latLonSplit[3]);
  }
  else if (latLonSplit.Length != 2)
  {
    throw new ArgumentException("Wrong number of arguments");
  }
  
  double latitude = 0;
  double longitude = 0;
  
  if (!double.TryParse(latLonSplit[0], NumberStyles.Any, NumberFormatInfo.InvariantInfo, out latitude))
  {
    throw new Exception(string.Format("Could not convert latitude to double: {0}", latLonSplit[0]));
  }
  
  if (!double.TryParse(latLonSplit[1], NumberStyles.Any, NumberFormatInfo.InvariantInfo, out longitude))
  {
    throw new Exception(string.Format("Could not convert longitude to double: {0}", latLonSplit[0]));
  }
  
  return new Vector2d(latitude, longitude);
}

arsenikstiger avatar Jul 11 '21 13:07 arsenikstiger