按下按钮添加结束标记

时间:2018-03-13 14:58:35

标签: android google-maps-api-3 google-maps-markers

我有一个Android应用程序,它获取我的位置并在其上显示一个标记。它还可以在本地跟踪设备位置并在其上绘制和标记,它还将标记与多边形线连接起来(这仅用于精度测试)。

我的问题是,当我按下完成旅程按钮时,如何让它显示标记结尾位置或类似的标记。

@Override
public void onLocationChanged(final Location location) {

    //Adds a marker on the current position found in LatLng
    final LatLng myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
    LocationMarker.setPosition(myCoordinates);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(myCoordinates));
    float zoomLevel = 12.0f;
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myCoordinates, zoomLevel));

    //Adds marker on each location update
    points.add(myCoordinates);
    mMap.addMarker(new MarkerOptions().position(myCoordinates));

        mMap.addPolyline(new PolylineOptions()
                .addAll(points)
                .width(5)
                .color(Color.RED));

    //Button used to get and display ending location
    Button stopLocBtn;
    stopLocBtn = (Button) findViewById(R.id.stopLocBtn);
    stopLocBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final LatLng myendCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
            points.add(myendCoordinates);

            LocationMarker.setPosition(myendCoordinates);
            mMap.addMarker(new MarkerOptions()
                    .position(myendCoordinates)
                    .title("End location"));

            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, TrackingActivity.this);
        }
    });
}

这是我的onLocationChanged函数的样子。我的位置更新保存在名为points的Global ArrayList中。

Sample of what it looks like when ran

2 个答案:

答案 0 :(得分:0)

  1. You put LatLng myCoordinates as a global variable to store last location in onLocationChange

    LatLng myCoordinates;
    @Override
    public void onLocationChanged(final Location location) {
      myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
    });

  1. Draw marker when click finished journey button
  2. Declare finished button in onCreate of Activity

//Button used to get and display ending location
    Button stopLocBtn;
    stopLocBtn = (Button) findViewById(R.id.stopLocBtn);
    stopLocBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            points.add(myCoordinates);

            LocationMarker.setPosition(myCoordinates);
            mMap.addMarker(new MarkerOptions()
                    .position(myCoordinates)
                    .title("End location"));

            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, TrackingActivity.this);
        }
    });

答案 1 :(得分:-1)

您的意思是创建不同颜色或样式的标记吗?您可以在MakerOptions上设置图标。

尝试使用BitmapDescriptorFactory.HUE_xxx值之一创建带有BitmapDescriptorFactory.defaultMarker(float hue)的新图标。