Forum Xamarin.Android

Convert String to LatLng

I'm using Google Maps API v2 and I get a location coordinates single String "-34.8799074,174.7565664" from my SharedPreferences that I need to convert to LatLng.

coordinatesText.Text = locations.tCoordinates; // coordinatesText.Text ="-34.8799074,174.7565664";

Can anyone help me to split the string?




  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭
    edited December 2016

    @Nanii Maps returns lat and long separately so you must save them into shared preferences. Than you can easily convert string to long like this :

    long lat = Convert.ToDouble("-34.8799074");
    long _long = Convert.ToDouble("174.7565664");

    Then :

    LatLng latlng = new LatLng(lat, _long);
  • NaniiNanii USMember

    Maps returns lat and long separately.but I stored these values in Database as a Single string. After that I got these values from db and stored in TextView. I need to convert to LatLng.
    coordinatesText.Text = locations.tCoordinates;

    above Text contain both Lat & Lang values.

    How to seperate Lat & Lang values

  • lukasrein97lukasrein97 USMember ✭✭✭
    edited December 2016

    LatLng latlng = new LatLng(lat, _long);
    This is the string now in latlng: latlng = "-34.8799074, 174.7565664"
    So if you want to split it you could do it with Regex.
    Create to regex the first one deletes all befor the "," in your string.
    The second one deletes all after your "," in your string.
    Dont forget to replace "," with "" after you did that regex for both.
    Then youll have both numbers seperated.
    I dont know atm but it could be that you must convert your latlng into a string befor, but i really dont know.

Sign In or Register to comment.