Android 中的反向地理编码

发布于 2024-10-22 09:36:57 字数 2169 浏览 2 评论 0原文

在下面的代码中,我收到以下异常

没有可用的服务

public class ds extends Activity {
    LocationManager locationManager;
    double lati,longi;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String location_context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(location_context);
        testProviders();
    }

    public void testProviders() {
        TextView tv = (TextView)findViewById(R.id.myTextView);
        StringBuilder sb = new StringBuilder("Enabled Providers:");
        List<String> providers = locationManager.getProviders(true);
        for (String provider : providers) 
        {
            locationManager.requestLocationUpdates(provider, 1000, 0,new LocationListener()
            {
                public void onLocationChanged(Location location) {}
                public void onProviderDisabled(String provider){}
                public void onProviderEnabled(String provider){}
                public void onStatusChanged(String provider, int status,
                    Bundle extras){}
            });
            sb.append("\n").append(provider).append(":");
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null)
            {
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                sb.append(lat).append(",").append(lng);
                lati=lat;
                longi=lng;

                Geocoder gcd = new Geocoder(ds.this, Locale.getDefault());
                List<Address> addresses;
                try {
                    addresses = gcd.getFromLocation(lati, longi, 1);
                    if (addresses.size() > 0) 
                } catch (IOException e) {
                    Toast.makeText(ds.this, "hi exception", 5000).show();
                }


            } 
            else {
                sb.append("No Location");
            }
        }
        tv.setText(sb);
    }
}

In the code below, I am getting the following exception

NO SERVICE AVAIALBLE

public class ds extends Activity {
    LocationManager locationManager;
    double lati,longi;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String location_context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(location_context);
        testProviders();
    }

    public void testProviders() {
        TextView tv = (TextView)findViewById(R.id.myTextView);
        StringBuilder sb = new StringBuilder("Enabled Providers:");
        List<String> providers = locationManager.getProviders(true);
        for (String provider : providers) 
        {
            locationManager.requestLocationUpdates(provider, 1000, 0,new LocationListener()
            {
                public void onLocationChanged(Location location) {}
                public void onProviderDisabled(String provider){}
                public void onProviderEnabled(String provider){}
                public void onStatusChanged(String provider, int status,
                    Bundle extras){}
            });
            sb.append("\n").append(provider).append(":");
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null)
            {
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                sb.append(lat).append(",").append(lng);
                lati=lat;
                longi=lng;

                Geocoder gcd = new Geocoder(ds.this, Locale.getDefault());
                List<Address> addresses;
                try {
                    addresses = gcd.getFromLocation(lati, longi, 1);
                    if (addresses.size() > 0) 
                } catch (IOException e) {
                    Toast.makeText(ds.this, "hi exception", 5000).show();
                }


            } 
            else {
                sb.append("No Location");
            }
        }
        tv.setText(sb);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谢绝鈎搭 2024-10-29 09:36:57

反向地理编码代码,您可以根据您的需求传递经纬度......

public class MainActivity extends FragmentActivity {

        static final LatLng DELHI = new LatLng(39.6985207, -104.8954315);
        GoogleMap map;
        Button btn_geo;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activitymain);

            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            btn_geo=(Button)findViewById(R.id.btn_getAddress);
            map.addMarker(new MarkerOptions().position(DELHI).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
            // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(DELHI, 15));

            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

            btn_geo.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
                    try {
                        if(myLocation.isPresent())
                        {
                            List<Address> addresses=null ;
                            addresses = myLocation.getFromLocation(39.6985207, -104.8954315, 1);
                            System.out.println(".................."+addresses);
                            StringBuilder sb = new StringBuilder();
                            if (addresses.size() > 0) 
                            {
                                Address address = addresses.get(0);
                                sb.append(address.getAddressLine(0)).append("\n");
                                sb.append(address.getLocality()).append("\n");
                                sb.append(address.getPostalCode()).append("\n");
                                sb.append(address.getCountryName());
                                Toast.makeText(getApplicationContext(), sb,Toast.LENGTH_LONG).show();
                            }
                        }
                        else
                            Toast.makeText(getApplicationContext(), "Not present",Toast.LENGTH_SHORT).show();
                    }
                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }

Code for Reverse Geocoding , you can pass the lattitude and longitude according to your requirement......

public class MainActivity extends FragmentActivity {

        static final LatLng DELHI = new LatLng(39.6985207, -104.8954315);
        GoogleMap map;
        Button btn_geo;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activitymain);

            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            btn_geo=(Button)findViewById(R.id.btn_getAddress);
            map.addMarker(new MarkerOptions().position(DELHI).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
            // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(DELHI, 15));

            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

            btn_geo.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
                    try {
                        if(myLocation.isPresent())
                        {
                            List<Address> addresses=null ;
                            addresses = myLocation.getFromLocation(39.6985207, -104.8954315, 1);
                            System.out.println(".................."+addresses);
                            StringBuilder sb = new StringBuilder();
                            if (addresses.size() > 0) 
                            {
                                Address address = addresses.get(0);
                                sb.append(address.getAddressLine(0)).append("\n");
                                sb.append(address.getLocality()).append("\n");
                                sb.append(address.getPostalCode()).append("\n");
                                sb.append(address.getCountryName());
                                Toast.makeText(getApplicationContext(), sb,Toast.LENGTH_LONG).show();
                            }
                        }
                        else
                            Toast.makeText(getApplicationContext(), "Not present",Toast.LENGTH_SHORT).show();
                    }
                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文