Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用纬度和经度在构造函数中设置LatLng位置_Java_Android_Google Maps - Fatal编程技术网

Java 使用纬度和经度在构造函数中设置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;

我是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);
}
}

使用obj.getLocation()获取位置