使用纬度和经度在构造函数中设置 LatLng 位置



我是Android编程的初学者。我想创建包含双倍纬度和经度的位置对象,并且使用纬度和经度作为输入来设置 LatLng 值。

我现在遇到的问题是我创建了一个以纬度和经度作为参数的对象,但 LatLng 似乎为空。(当我想使用 LatLng 值设置标记时,会出现运行时错误(。使用纬度和经度作为参数创建对象时如何设置 LatLng 值? 这是我的代码:

public class Location {
int id;
double latitude;
double longitude;
LatLng location;
public Location() {}
public Location(int id, double latitude, double longitude) {
this.id = id;
this.latitude = latitude;
this.longitude = longitude;
this.location = new LatLng(latitude, longitude);
}
public Location(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
this.location = new LatLng(latitude, longitude);
}
public void setId(int id) {
this.id = id;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public void setLocation(LatLng location) {
this.location = location;
}
public int getId() {
return this.id;
}
public double getLatitude() {
return this.latitude;
}
public double getLongitude() {
return this.longitude;
}
public LatLng getLocation() {
return this.location;
}
}

只需更改为此内容即可对您有所帮助。

public class Location {
int id;
double latitude;
double longitude;
LatLng location;
public Location() {}
public Location(int id, double latitude, double longitude) {
this.id = id;
this.latitude = latitude;
this.longitude = longitude;
}
public Location(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public void setId(int id) {
this.id = id;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public void setLocation(LatLng location) {
this.location = location;
}
public int getId() {
return this.id;
}
public double getLatitude() {
return this.latitude;
}
public double getLongitude() {
return this.longitude;
}
public LatLng getLocation() {
return new LatLng(latitude, longitude);
}
}

最新更新