【问题标题】:how to find longitude and latitude of particular location?如何找到特定位置的经度和纬度?
【发布时间】:2011-09-09 04:01:17
【问题描述】:

如何查找特定位置的经度和纬度?

用户在edittext中输入位置并单击搜索按钮,然后该位置将显示在googlemap中。

我为此使用了以下代码,但这会给出错误“服务不可用”

地理编码器 geoCoder = new Geocoder(this, Locale.getDefault()); 尝试 { 地址=geoCoder.getFromLocationName(txtlocation.getText().toString(), 1).get(0); 双 longi=address.getLongitude(); 双纬度=地址.getLatitude(); System.out.println("经度:--- " +longi); System.out.println("纬度:---" +latit); } 捕捉(IOException e){ // TODO 自动生成的 catch 块 e.printStackTrace(); Toast.makeText(MapRouteActivity.this, e.toString(), Toast.LENGTH_LONG).show(); }

【问题讨论】:

  • 你为什么不使用 locationManager。
  • 在我的应用程序中,我使用了 locationmanager,但这里没有显示。
  • 先仰望夜空,找到北极星.... ;)

标签: android


【解决方案1】:

尝试使用

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); double longitude = location.getLongitude(); double latitude = location.getLatitude(); 

对 getLastKnownLocation() 的调用不会阻塞 - 这意味着如果当前没有可用的位置,它将返回 null - 所以您可能想看看将 LocationListener 传递给 requestLocationUpdates() 方法,这将给出你异步更新你的位置。

private final LocationListener locationListener = new LocationListener() {     public void onLocationChanged(Location location) {         longitude = location.getLongitude();         latitude = location.getLatitude();     } } 

lm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener); 

如果您想使用 GPS,您需要为您的应用授予 ACCESS_FINE_LOCATION 权限。

您可能还想在 GPS 不可用时添加 ACCESS_COARSE_LOCATION 权限,并使用 getBestProvider() 方法选择您的位置提供者。

【讨论】:

    【解决方案2】:

    试试这个代码

    public static String getLatLng(Context context,String addr){
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        String add = "";
        try{
            List<Address> addresses = geocoder.getFromLocationName(addr, 5);
    
            for(int i=0;i<1;i++){
                Address obj = addresses.get(i);
                for(int j=0;j<obj.getMaxAddressLineIndex();j++){
                    add = obj.getAddressLine(j);
                    add = add + "\nCountryName " + obj.getCountryName();
                    add = add + "\nCountryCode " + obj.getCountryCode();
                    add = add + "\nAdminArea " + obj.getAdminArea();
                    add = add + "\nPostalCode " + obj.getPostalCode();
                    add = add + "\nSubAdminArea " + obj.getSubAdminArea();
                    add = add + "\nFeatureName " + obj.getFeatureName();
                    add = add + "\nLocality " + obj.getLocality();
                    add = add + "\n" + obj.getSubThoroughfare();
                    add = add + "\nurl " + obj.getUrl();
                    add = add + "\nLatitude " + obj.getLatitude();
                    add = add + "\nLongitude " + obj.getLongitude();
                }
                add = add+"\n";
            }
    
            Log.v("IGA", "Address" + add);
    
        }catch(Exception e){
            e.printStackTrace();
            add = e.toString();
        }
        return add;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多