如何从对象信息中将自定义标记放置在google-maps活动中

时间:2017-09-07 18:42:25

标签: java android google-maps

我想在特定位置放置标记。起初,我认为这样做很简单,但不知何故我感到困惑,而且我还没有找到能真正满足我需要的东西。我已经尝试将我的活动建立在这段视频的基础上here

我的活动从接收getIntent()的对象信息开始,然后我提取信息。在那个对象(在这种情况下,一个汽车),有很多属性,其中2个是纬度和经度。前面的是Double值。

我面临的问题是我不知道如何将从对象获得的信息输入到程序中。我将代码放在下面。有什么建议吗?

public class CarDataset implements Parcelable
{

int vehicleID;
String model;
String licencePlate;
String brand;
Double latitude;
Double longitude;

public CarDataset(Integer nVehicleID, String nModel, String nLicencePlate, String nBrand, Double nLatitude, Double nLongitude)
{
    this.vehicleID = nVehicleID;
    this.model = nModel;
    this.licencePlate = nLicencePlate;
    this.brand = nBrand;
    this.latitude = nLatitude;
    this.longitude = nLongitude;
}

public int getVehicleID() {
    return vehicleID;
}

public void setVehicleID(int vehicleID) {
    this.vehicleID = vehicleID;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public String getLicencePlate() {
    return licencePlate;
}

public void setLicencePlate(String licencePlate) {
    this.licencePlate = licencePlate;
}

public String getBrand() {
    return brand;
}

public void setBrand(String brand) {
    this.brand = brand;
}

public Double getLatitude() {
    return latitude;
}

public void setLatitude(Double latitude) {
    this.latitude = latitude;
}

public Double getLongitude() {
    return longitude;
}

public void setLongitude(Double longitude) {
    this.longitude = longitude;
}

@Override
public int describeContents()
{
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags)
{
    dest.writeInt(vehicleID);
    dest.writeString(model);
    dest.writeString(licencePlate);
    dest.writeString(brand);
    dest.writeDouble(latitude);
    dest.writeDouble(longitude);
}

public static final Parcelable.Creator<CarDataset> CREATOR
        = new Parcelable.Creator<CarDataset>()
{
    public CarDataset createFromParcel(Parcel input)
    {
        return new CarDataset(input);
    }

    public CarDataset[] newArray(int size)
    {
        return new CarDataset[size];
    }
};

private CarDataset(Parcel input)
{
    vehicleID = input.readInt();
    model = input.readString();
    licencePlate = input.readString();
    brand = input.readString();
    latitude = input.readDouble();
    longitude = input.readDouble();
}

这是我使用的CarDataset类:

{{1}}

}

2 个答案:

答案 0 :(得分:0)

LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomBy(3));

mMap.addMarker(new MarkerOptions()
.title("Here!")
.icon(BitmapDescriptorFactory.defaultMarker())
.position(latlng_object));

答案 1 :(得分:0)

您需要做的就是创建一个LatLng对象,其中包含您从Intent获取的CarDataset对象的纬度和经度。

Marker carMarker;
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (cardata != null) {
        LatLng latLngCar = new LatLng(cardata.latitude, cardata.longitude);
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLngCar);
        markerOptions.title("Car");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker());

        carMarker = mMap.addMarker(markerOptions);
    }


    //.............

}

您还需要修改onCreate(),以便cardata是一个实例变量:

CarDataset cardata;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_geolocalizcao);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.

    Intent i = getIntent();

    //use instance variable:
    cardata = (CarDataset) i.getExtras().getParcelable("select");

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}