-1

The Location value is getting from our server, in string format now I define new location after that i want to get the longitude and latitude but it's 0.0. Code is like this.

String location = "lat\/lng: (28.6988812,77.1153696)";
Location loc = new Location(location);
double lat = loc.getLatitude();
double longitude = loc.getLongitude();

2 Answers2

0

As per Android documentation you need to pass location provider in the constructor. Refer https://developer.android.com/reference/android/location/Location.html#Location(java.lang.String)

You need to parse the string ("lat/lng: (28.6988812,77.1153696)") that you are getting from you server, extract latitude and longitude and pass those values to your location object using setLatitude() and setLongitude() function.

Raziya Bhagat
  • 216
  • 2
  • 8
0

You can extract latitude and longitude from the location string you got from the server with regex. Like this:

Double latitude = 0., longitude = 0.;

    //your location
    String location = "lat/long: (28.6988812,77.1153696)";
    //pattern
    Pattern pattern = Pattern.compile("lat/long: \\(([0-9.]+),([0-9.]+)\\)$");
    Matcher matcher = pattern.matcher(location);
    if (matcher.matches()) {
        latitude = Double.valueOf(matcher.group(1));
        longitude = Double.valueOf(matcher.group(2));
    }

if you need a Location object:

Location targetLocation = new Location("");//provider name is unnecessary
targetLocation.setLatitude(latitude);//your coords of course
targetLocation.setLongitude(longitude);

thanks to this answer

maheryhaja
  • 1,617
  • 11
  • 18