我正在创建一个Android应用程序。目前,该位置是硬编码的。经度和纬度在oncreate方法中声明。我创建了一种方法来获取oncreate方法之外的经度和纬度。
这是oncreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_home );
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
getLocation();
//Doublin Co-ordinates
final double latitude = 53.3498;
final double longitude = -6.2603;
//Getting th data
getForecast(latitude, longitude);
Log.d(Tag,"MAIN UI code running");
}
您可以看到latitude
和latitude
变量中的位置是硬编码的。
我还创建了一种获取用户手机最后已知坐标的方法。这不是oncreate方法。
void getLocation() {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null){
double latti = location.getLatitude();
double longi = location.getLongitude();;
} else {
Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();
}
}
}
如何在位置方法中将latti
和longi
的值分配给longitude
方法中的latitude
和oncreate
。
答案 0 :(得分:0)
如何在该位置分配latti和longi的值 方法,
WebView
方法中的经度和纬度。
我建议你将纬度和经度声明为全局变量(超出WebView
方法)
SMWebView
然后在oncreate
函数
onCreate
或者您可以将这两个变量传递给final double latitude = 53.3498;
final double longitude = -6.2603;
方法。
getLocation
以这种方式修改 if (location != null){
double latti = latitude ;
double longi = longitude;
} else {
Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();
}
功能
getLocation
答案 1 :(得分:0)
最好的方法是将字段声明为全局字段,然后将值分配给字段,然后在onCreate()
或您想要的位置使用它。
从getLocation()
onCreate() {
....
Pair<Double, Double> locationInfo = getLocation();
final double latitude = locationInfo.first;
final double longitude = locationInfo.second;
}
Pair<Double, Double> getLocation(){
....
double latti = location.getLatitude();
double longi = location.getLongitude();
return new Pair<Double, Double>(latti, longi);
}
您可以使用getLocation()
方法执行位置实例,并在onCreate()
答案 2 :(得分:0)
2.在调用设置getLocation()
和latti
值的方法longi
后,设置latitude
和longitude
的值。请参阅代码
请尝试以下代码:
public class MainActivity extends Activity
{
double latti; //new code
double longi; //new code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_home );
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
getLocation();
//Doublin Co-ordinates
final double latitude = latti; //i changed this line
final double longitude = longi;//i changed this line
//Getting th data
getForecast(latitude, longitude);
Log.d(Tag,"MAIN UI code running");
} //end of onCreate
void getLocation() {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null){
latti = location.getLatitude();//i changed this line, (drooped the double)
longi = location.getLongitude();//i changed this line, (drooped the double)
} else {
Toast.makeText( this, "Unable to get location. Please try again:", Toast.LENGTH_LONG ).show();
}
}
}
} //end of MainActvity
另一种解决方案是吸气剂和放大器。 setter方法。 (旁注:你不需要纬度和经度,你可以只使用latti和longi)