移动时画一条折线

时间:2018-08-08 19:11:11

标签: android polyline

我想做我的第一个应用程序,以便在我移动(路线跟踪)时在地图上添加一条折线。我想在按下ButtonStart时开始绘制折线,而在按下ButtonEnd时停止绘制折线。当我运行它时,它不会绘制折线。

我在手机中运行它,并断开了与电脑的连接,因此我可以移动并查看它是否会绘制折线。

public class MapsActivityCreateNewPath extends FragmentActivity implements
    OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener, View.OnClickListener {

private GoogleMap mMap;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private Location lastLocation;
private Marker currentUserLocationMarker;
private static final int Request_User_Location_Code = 99;
private ArrayList<LatLng> listPoints;
Polyline line;

int count = 0;
Button ButtonStart;
Button ButtonEnd;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps_create_new_path);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        checkUserLocationPermission();
    }

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    listPoints = new ArrayList<LatLng>();


    ButtonStart = (Button) findViewById(R.id.ButtonStart);
    ButtonEnd = (Button) findViewById(R.id.ButtonEnd);

    if (count == 0)
    {
        ButtonEnd.setEnabled(false);
        ButtonStart.setEnabled(true);
    }

    ButtonStart.setOnClickListener(new View.OnClickListener(){public void onClick (View v) {next_page(v);}});
    ButtonEnd.setOnClickListener(new View.OnClickListener() {public void onClick (View v) {next_page(v);}});



    }





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

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
    {

        buildGoogleApiClient();

        mMap.setMyLocationEnabled(true);
    }

}


public boolean checkUserLocationPermission()
{
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION))
        {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Request_User_Location_Code);
        }
        else
        {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Request_User_Location_Code);
        }
        return false;
    }
    else
    {
        return true;
    }
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    switch (requestCode)
    {
        case Request_User_Location_Code:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
                {
                    if (googleApiClient == null)
                    {
                        buildGoogleApiClient();
                    }
                    mMap.setMyLocationEnabled(true);
                }
            }
            else
            {
                Toast.makeText(this, "Permission Denied...", Toast.LENGTH_SHORT).show();
            }
            return;
    }
}

protected synchronized void buildGoogleApiClient()
{
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        googleApiClient.connect();
}

        @Override
        public void onLocationChanged(Location location) {

        lastLocation = location;

        if (currentUserLocationMarker != null)
        {
            currentUserLocationMarker.remove();
        }

            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("user Current Location");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));

            currentUserLocationMarker = mMap.addMarker(markerOptions);

            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            mMap.animateCamera(CameraUpdateFactory.zoomBy(14));

            if (googleApiClient != null)
            {
                LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
            }

            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            LatLng latLng1 = new LatLng(latitude, longitude);

            listPoints.add(latLng1);

            redrawLine();




        }


@Override
        public void onConnected(@Nullable Bundle bundle) {

        locationRequest = new LocationRequest();
        locationRequest.setInterval(1100);
        locationRequest.setFastestInterval(1100);
        locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
        }

        }

        @Override
        public void onConnectionSuspended(int i) {

        }

        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

        }


public void next_page(View v){


    switch(v.getId())
    {
        case R.id.ButtonStart:
            count++;
            ButtonStart.setEnabled(false);
            ButtonEnd.setEnabled(true);
            break;

        case R.id.ButtonEnd:
            count--;
            ButtonStart.setEnabled(true);
            ButtonEnd.setEnabled(false);
            break;
    }

    if (count == 1) {
        redrawLine();
    }
}

@Override
public void onClick(View v) {

}

private void redrawLine()
{
    if (count == 0)
    {
        return;
    }

    mMap.clear();

    PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
    for (int i = 0; i < listPoints.size(); i++)
    {
        LatLng point = listPoints.get(i);
        options.add(point);
    }

    line = mMap.addPolyline(options);
}
}

0 个答案:

没有答案