android - 如何从城市名称android代码获取经度,纬度

标签 android

我想转换从包含城市名称的文本字段中获取的文本,我想将其转换为经度和纬度。

这是我做的:

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);
            }

但是代码没有采用else语句

我将 URL 更改为:

String myUrl="http://maps.googleapis.com/maps/api/geocode/json?address="+location+"&sensor=true";

这是结果:

{
   "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"
}

如何在我的代码中使用纬度和经度?

最佳答案

使用 Geocoder 有一个更简单的方法.它的作用与地理编码 API 几乎相同。

if(Geocoder.isPresent()){
    try {
        String location = "theNameOfTheLocation";
        Geocoder gc = new Geocoder(this);
        List<Address> addresses= gc.getFromLocationName(location, 5); // get the found Address Objects

        List<LatLng> ll = new ArrayList<LatLng>(addresses.size()); // A list to save the coordinates if they are available
        for(Address a : addresses){
            if(a.hasLatitude() && a.hasLongitude()){
                ll.add(new LatLng(a.getLatitude(), a.getLongitude()));
            }  
        }  
    } catch (IOException e) {
         // handle the exception
    }
}

关于android - 如何从城市名称android代码获取经度,纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20166328/

相关文章:

android - 如何将 child 添加到 xml 中的 android 自定义 View /布局?

java - 如何以编程方式调整 android 中选项卡的文本的高度和粗体?

android - 如何访问Android设备上的图库文件夹

android - 什么是 INSTALL_PARSE_FAILED_NO_CERTIFICATES 错误?

android - Fragments 中的 getLoaderManager 已弃用

Android - 警告对话框 - 自制取消按钮

android - 带有 CheckBox 单选的自定义 Listview

android - ScrollView 内 TextView 中的超链接不可单击

java - Android Studio运行java类时出错

android - Activity 中的 fragment 在 ListView 的 setAdapter 中获取上下文 null