如何检查经纬度是否在特定城市内?

4
我想检查我的经纬度是否属于特定城市。 我从placeAutoComplete.IntentBuilder接收我的经纬度。
try {
  Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
    .setBoundsBias(new LatLngBounds(
      new LatLng(Double.valueOf(18.5204), Double.valueOf(73.8567)),
      new LatLng(Double.valueOf(18.5204), Double.valueOf(73.8567))))
    .build(CheckOut.this);

  startActivityForResult(intent, 1);
} catch (GooglePlayServicesRepairableException e) {
  // TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
  // TODO: Handle the error.
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == 1) {
    if (resultCode == RESULT_OK) {
      Place place = PlaceAutocomplete.getPlace(this, data);
      Log.i("place", "Place: " + place.getLatLng());
      LatLng placeLatLong= place.getLatLng();
      user_lat = String.valueOf(placeLatLong.latitude);
      user_long = String.valueOf(placeLatLong.longitude);
      tvAddress2Popup.setText(String.valueOf(place.getName()));
    } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
      Status status = PlaceAutocomplete.getStatus(this, data);
      // TODO: Handle the error.
      Log.i("", status.getStatusMessage());
    } else if (resultCode == RESULT_CANCELED) {
      // The user canceled the operation.
    }
  }
}

请给我解决方案

4个回答

0

0

0

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());

addresses = geocoder.getFromLocation(latitude, longitude, 1); 
String address = addresses.get(0).getAddressLine(0); 
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postal_Code = addresses.get(0).getPostalCode();
String Name = addresses.get(0).getFeatureName(); 


0

你可以像这样简单地检查

String latitudeStr = String.valueOf(address.getLatitude());
                latitudeStr = latitudeStr.substring ( latitudeStr.indexOf ( "." ) );

                String longitudeStr = String.valueOf(address.getLongitude());
                longitudeStr = longitudeStr.substring ( longitudeStr.indexOf ( "." ) );


                if(latitudeStr.length()>=8) {

                //this is city  
                } 

通过这个你可以获取城市的经纬度..


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接