Java 使用纬度和经度在构造函数中设置LatLng位置
我是Android编程的初学者。我想创建位置对象,它包含两倍的纬度和经度,LatLng值是使用纬度和经度作为输入设置的 我现在遇到的问题是,我创建了一个以纬度和经度为参数的对象,但LatLng似乎为空。(当我想使用LatLng值设置标记时,给出一个运行时错误)。使用纬度和经度作为参数创建对象时,如何设置LatLng值? 这是我的代码:Java 使用纬度和经度在构造函数中设置LatLng位置,java,android,google-maps,Java,Android,Google Maps,我是Android编程的初学者。我想创建位置对象,它包含两倍的纬度和经度,LatLng值是使用纬度和经度作为输入设置的 我现在遇到的问题是,我创建了一个以纬度和经度为参数的对象,但LatLng似乎为空。(当我想使用LatLng值设置标记时,给出一个运行时错误)。使用纬度和经度作为参数创建对象时,如何设置LatLng值? 这是我的代码: public class Location { int id; double latitude; double longitude;
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);
}
}
使用obj.getLocation()获取位置