I want to convert a text taken from a text field which contains the city name, and I want to convert it to longitude and latitude.
this is what I made:
String location=city.getText().toString();
String inputLine = "";
String result = "";
location=location.replaceAll(" ", "%20");
String myUrl="http://maps.google.com/maps/geo?q="+location+"&output=csv";
try{
URL url=new URL(myUrl);
URLConnection urlConnection=url.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
while ((inputLine = in.readLine()) != null) {
result=inputLine;
}
lat = result.substring(6, result.lastIndexOf(","));
longi = result.substring(result.lastIndexOf(",") + 1);
}
catch(Exception e){
e.printStackTrace();
}
//////////////////////////////////
if (location=="" )
{
latitude=loc.getLatitude();
longitude=loc.getLongitude();
}
else
{
latitude=Double.parseDouble(lat);
longitude=Double.parseDouble(longi);
}
but the code doesn't take the else statement
I changed the URL to this:
String myUrl="http://maps.googleapis.com/maps/api/geocode/json?address="+location+"&sensor=true";
and this is the result:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Nablus",
"short_name" : "Nablus",
"types" : [ "locality", "political" ]
}
],
"formatted_address" : "Nablus",
"geometry" : {
"location" : {
"lat" : 32.22504,
"lng" : 35.260971
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 32.2439165,
"lng" : 35.2929858
},
"southwest" : {
"lat" : 32.20615960000001,
"lng" : 35.2289562
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
how could I use latitude and longitude in my code??