我是Android编程的初学者。我想创建包含双倍纬度和经度的位置对象,并使用纬度和经度作为输入设置LatLng值。
我现在遇到的问题是我创建了一个以纬度和经度为参数的对象,但LatLng似乎为null。 (当我想使用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;
}
}
答案 0 :(得分:0)
改变这个对你有帮助。
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);
}
}