从纬度和经度的当前位置 [英] Current location from latitude and longitude

查看:152
本文介绍了从纬度和经度的当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用两种方法,它的getAddress()和()的地址。其中获得纬度和等一个从经纬度获得地址。

I am using two methods for it getAddress() and address()..One to get latitude and and other one to get address from latitude and longitude..

       public String  getAddress(){

       location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
       location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);

            latitude= location.getLatitude();
            longitude= location.getLongitude();

        try {
            val = address(latitude, longitude);
            } catch (IOException e) {

            e.printStackTrace();
            }


            Toast.makeText( CurrentLoc.this, val,
                    Toast.LENGTH_LONG).show();

        return val;
    }        

使用另一种方法来获得地址():

Using another method to get address():

       public String address(double lt,double lg) throws IOException{
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this, Locale.getDefault());
        addresses = geocoder.getFromLocation(lt, lg, 1);

        String address = addresses.get(0).getAddressLine(0);
        String city = addresses.get(0).getAddressLine(1);
        String country = addresses.get(0).getAddressLine(2);
        return address +"\n"+ city +"\n"+ country;
    }       

要使用InitializeMap()在地图上获取当前位置:

To get current location on map using InitializeMap():

    private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(location.getLatitude(), location.getLongitude())).zoom(12).build();

        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        MarkerOptions marker = new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Hello Maps ");
        googleMap.addMarker(marker);
        googleMap.isMyLocationEnabled();
        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

和全code为以下,但问题是,没有得到地址:

And the full code is following but the problem is that not getting address:

 public class CurrentLoc extends Activity {

// latitude and longitude
static double latitude ;
static double longitude ;

  // Google Map
private GoogleMap googleMap;
private LocationManager locationManager;
private Location location;
private String val,val1;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new3);
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()
        );  
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());

      val1 = getAddress();


        // Loading map
        initilizeMap();

}

/**
 * function to load map. If map is not created it will create it for you
 * */

public String  getAddress(){

       location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
       location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);

            latitude= location.getLatitude();
            longitude= location.getLongitude();

        try {
            val = address(latitude, longitude);
            } catch (IOException e) {

            e.printStackTrace();
            }


            Toast.makeText( CurrentLoc.this, val,
                    Toast.LENGTH_LONG).show();

        return val;
    }      

    public String address(double lt,double lg) throws IOException{
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this, Locale.getDefault());
        addresses = geocoder.getFromLocation(lt, lg, 1);

        String address = addresses.get(0).getAddressLine(0);
        String city = addresses.get(0).getAddressLine(1);
        String country = addresses.get(0).getAddressLine(2);
        return address +"\n"+ city +"\n"+ country;
    }       






    private class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
       Toast.makeText(CurrentLoc.this, message, Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(CurrentLoc.this, "Provider status changed",
               Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(CurrentLoc.this,
                "Provider disabled by the user. GPS turned off",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(CurrentLoc.this,
                "Provider enabled by the user. GPS turned on",
                Toast.LENGTH_LONG).show();
    }

    }



@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(location.getLatitude(), location.getLongitude())).zoom(12).build();

googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        MarkerOptions marker = new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Hello Maps ");
        googleMap.addMarker(marker);
        googleMap.isMyLocationEnabled();
        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.mnew1, menu);

            return super.onCreateOptionsMenu(menu);
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle presses on the action bar items
            switch (item.getItemId()) {
                case R.id.home:

                    openSearch();

                    return true;          
            default:
                    return super.onOptionsItemSelected(item);
            }
            }

        private void openSearch(){

            Intent intnt=new Intent(getApplicationContext(),SendSms.class);

            intnt.putExtra("loct", val1);

            startActivity(intnt); 
        }

}

推荐答案

首先,你需要做的的谷歌搜索:

First you have to do Googling:

您将获得以下链接:

这篇关于从纬度和经度的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆