Java 如何在编辑文本中设置位置在我的代码中从地图获取当前位置
公共类UpdateShippingAddressActivity扩展了AppCompatActivity{Java 如何在编辑文本中设置位置在我的代码中从地图获取当前位置,java,android,google-maps,Java,Android,Google Maps,公共类UpdateShippingAddressActivity扩展了AppCompatActivity{ TextInputEditText et_firstName; TextInputEditText et_lastName; TextInputEditText et_address1; TextInputEditText et_city; TextInputEditText et_postalcode; TextInputEditText
TextInputEditText et_firstName;
TextInputEditText et_lastName;
TextInputEditText et_address1;
TextInputEditText et_city;
TextInputEditText et_postalcode;
TextInputEditText et_state;
TextInputEditText et_country;
TextView tv_getPin;
Spinner places;
String strAddress2;
LocationManager nManager;
FusedLocationProviderClient fusedLocationProviderClient;
private APIInterface apiInterface;
private Dialog progressDialog;
Button btn_update;
String shippingId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_shipping_address);
et_firstName = findViewById(R.id.et_name);
et_lastName = findViewById(R.id.et_lastName);
et_address1 = findViewById(R.id.et_address1);
tv_getPin = findViewById(R.id.tv_getPin);
et_city = findViewById(R.id.et_city);
et_postalcode = findViewById(R.id.et_postalcode);
et_state = findViewById(R.id.et_state);
et_country = findViewById(R.id.et_country);
places = findViewById(R.id.places);
btn_update = findViewById(R.id.btn_update);
String apiKey = "AIzaSyBnySnbgwp1r3ZQOtKMl8nig3tPwR9j4To";
nManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Intent intent = getIntent();
shippingId = intent.getStringExtra("shippingId");
String firstName = intent.getStringExtra("firstname");
String lastName = intent.getStringExtra("lastname");
String address1 = intent.getStringExtra("housename");
String address2 = intent.getStringExtra("addressmap");
String city = intent.getStringExtra("city");
String postalCode = intent.getStringExtra("postalcode");
String state = intent.getStringExtra("state");
String country = intent.getStringExtra("country");
String category = intent.getStringExtra("homeoroffice");
et_firstName.setText(firstName);
et_lastName.setText(lastName);
et_address1.setText(address1);
tv_getPin.setText(address2);
et_city.setText(city);
et_postalcode.setText(postalCode);
et_state.setText(state);
et_country.setText(country);
tv_getPin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(UpdateShippingAddressActivity.this, MapsActivity.class);
i.putExtra("check", true);
startActivityForResult(i, 1);
// startActivity(intent);
}
});
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location != null) {
location.getLongitude();
Log.d("TAG", "onComplete: " + location.getLatitude() + "\n" + location.getLongitude());
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(UpdateShippingAddressActivity.this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
} catch (IOException e) {
e.printStackTrace();
}
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
Log.d("TAG", "onClick: " + address + "\n" + city + "\n" + state + "\n" + country + "\n" + postalCode + "\n" + knownName);
if (country.equals("Pakistan")) {
tv_getPin.setText(address);
et_postalcode.setText(postalCode);
et_country.setText(country);
et_state.setText(state);
et_city.setText(city);
} else {
Toast.makeText(getApplicationContext(), "Service Not Available in your Region", Toast.LENGTH_SHORT).show();
}
}
}
});
btn_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateShippingAddress();
}
});
}
// private void getLocation()
// {
//
// Geocoder geocoder;
// List<Address> addresses = null;
// geocoder = new Geocoder(UpdateShippingAddressActivity.this, Locale.getDefault());
// try {
// addresses = geocoder.getFromLocation(, location.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
// } catch (IOException e) {
//
// e.printStackTrace();
// }
// }
}
textinputtext et_firstName;
text输入text et_lastName;
text输入text et_地址1;
text输入text et_城市;
文本输入文本等后代码;
text输入text et_状态;
text输入text等国家/地区;
TextView tv_getPin;
旋转器位置;
字符串2;
地点经理;
FusedLocationProviderClient FusedLocationProviderClient;
专用接口;
私人对话;
按钮btn_更新;
字符串shippingId;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u update\u shipping\u address);
et_firstName=findviewbyd(R.id.et_name);
et_lastName=findviewbyd(R.id.et_lastName);
et_address1=findviewbyd(R.id.et_address1);
tv_getPin=findviewbyd(R.id.tv_getPin);
et_city=findviewbyd(R.id.et_city);
et_postalcode=findViewById(R.id.et_postalcode);
et_状态=findViewById(R.id.et_状态);
et_国家=findViewById(R.id.et_国家);
地点=findViewById(R.id.places);
btn_update=findviewbyd(R.id.btn_update);
字符串apiKey=“AIzaSyBnySnbgwp1r3ZQOtKMl8nig3tPwR9j4To”;
nManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
返回;
}
Intent=getIntent();
shippingId=intent.getStringExtra(“shippingId”);
String firstName=intent.getStringExtra(“firstName”);
String lastName=intent.getStringExtra(“lastName”);
字符串地址1=intent.getStringExtra(“房屋名称”);
String address2=intent.getStringExtra(“addressmap”);
String city=intent.getStringExtra(“城市”);
字符串postalCode=intent.getStringExtra(“postalCode”);
字符串状态=intent.getStringExtra(“状态”);
字符串国家=intent.getStringExtra(“国家”);
字符串类别=intent.getStringExtra(“homeoprofice”);
et_firstName.setText(firstName);
et_lastName.setText(lastName);
et_address1.setText(address1);
tv_getPin.setText(地址2);
et_city.setText(城市);
et_postalcode.setText(postalcode);
et_state.setText(状态);
et_country.setText(国家);
tv_getPin.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意图i=新意图(UpdateShippingAddressActivity.this、MapsActivity.class);
i、 putExtra(“检查”,正确);
startActivityForResult(i,1);
//星触觉(意向);
}
});
fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(此);
fusedLocationProviderClient.getLastLocation().addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
位置=task.getResult();
如果(位置!=null){
location.getLongitude();
Log.d(“TAG”,“onComplete:”+location.getLatitude()+“\n”+location.getLatitude());
地理编码器;
列表地址=空;
geocoder=新的geocoder(UpdateShippingAddressActivity.this,Locale.getDefault());
试一试{
addresses=geocoder.getFromLocation(location.getLatitude(),location.getLatitude(),1);//这里1表示返回的最大位置结果,由它建议的1到5个文档表示
}捕获(IOE异常){
e、 printStackTrace();
}
String address=addresses.get(0).getAddressLine(0);//如果存在除此之外的任何其他地址行,请通过getMaxAddressLineIndex()检查最大可用地址行
字符串city=addresses.get(0.getLocation();
字符串状态=addresses.get(0.getAdminArea();
字符串country=addresses.get(0.getCountryName();
字符串postalCode=addresses.get(0.getPostalCode();
字符串knownName=addresses.get(0.getFeatureName();
Log.d(“标记”、“onClick:”+地址+“\n”+城市+“\n”+州+“\n”+国家+“\n”+邮政编码+“\n”+知识名);
如果(国家等于(“巴基斯坦”)){
tv_getPin.setText(地址);
et_postalcode.setText(postalcode);
et_country.setText(国家);
et_state.setText(状态);
et_city.setText(城市);
}否则{
Toast.makeText(getApplicationContext(),“您所在地区的服务不可用”,Toast.LENGTH\u SHORT.show();
}
}
}
});
btn_update.setOnClickListener(新视图.OnClickListener(){