title: how to get longitude,latitude from the city name android code
body: <p>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.</p>
  
  <p>this is what I made:</p>
  
  <pre><code>String location=city.getText().toString();
              String inputLine = "";
              String result = "";
              location=location.replaceAll(" ", "%20");
              String myUrl="http://maps.google.com/maps/geo?q="+location+"&amp;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);
              }
  </code></pre>
  
  <p>but the code doesn't take the     else statement</p>
  
  <p>I changed the URL to this:</p>
  
  <p>String myUrl="http://maps.googleapis.com/maps/api/geocode/json?address="+location+"&amp;sensor=true";</p>
  
  <p>and this is the result:</p>
  
  <pre><code>{
     "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"
  }
  </code></pre>
  
  <p>how could I use latitude and longitude in my code??</p>