我该如何设置这个标记的动画?

时间:2016-04-08 04:27:05

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

是的,有人可以帮我吗?我想动画“巴士”标记我已经看过很多视频和教程但是我不能让它工作我是android的新手,这是我完成我的应用程序时唯一缺少的东西,我想让标记穿过折线非常感谢你的帮助!

这是代码:

package com.example.alejandro.integradora2;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class RutaUtd extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
        LocationListener,
        OnMapReadyCallback {

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    TextView info;

    LatLng latLng;
    GoogleMap mGoogleMap;
    Location mLastLocation;
    Marker mCurrLocation, bus,finr;




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

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


    }



    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mGoogleMap.setMyLocationEnabled(true);

        buildGoogleApiClient();

        mGoogleApiClient.connect();

        // Add a marker in Sydney and move the camera
        LatLng inicio = new LatLng(23.989444, -104.617897);
        LatLng fin = new LatLng(24.032782, -104.639596);
        mGoogleMap.addMarker(new MarkerOptions().position(inicio).title("Inicio Ruta").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
        mGoogleMap.addMarker(new MarkerOptions().position(fin).title("Fin Ruta"));
        bus= mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(23.996506, -104.638496)).title("Ubicacion del Bus").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus)));
        //mMap.animateCamera(CameraUpdateFactory.zoomTo(10));

        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(24.005651, -104.600266), 12));

        PolylineOptions RutaIda = new PolylineOptions()
                .add(new LatLng(23.989444, -104.617897),new LatLng(23.996506, -104.638496),new LatLng(23.996741, -104.638694),new LatLng(23.998000, -104.638673),new LatLng(23.998720, -104.639027),
                        new LatLng(24.000460, -104.640336),new LatLng(24.001126, -104.641189),new LatLng(24.001479, -104.642369),new LatLng(24.002038, -104.643131),new LatLng(24.004086, -104.644274),
                        new LatLng(24.005674, -104.644124),new LatLng(24.006680, -104.644261),new LatLng(24.021361, -104.645530),new LatLng(24.026858, -104.645884),new LatLng(24.032508, -104.639628),
                        new LatLng(24.032831, -104.638727),new LatLng(24.032851, -104.636989),new LatLng(24.033086, -104.636957),new LatLng(24.033194, -104.639135),new LatLng(24.032782, -104.639596))
                .width(5)
                .color(Color.RED);
        Polyline polyline = mGoogleMap.addPolyline(RutaIda);

        PolylineOptions RutaVuelta = new PolylineOptions()
                .add(new LatLng(24.032782, -104.639596),new LatLng(24.027185, -104.645781),new LatLng(24.025901, -104.645888),new LatLng(24.006594, -104.644299),new LatLng(24.004761, -104.644396),
                        new LatLng(24.003761, -104.644267),new LatLng(24.002016, -104.643237),new LatLng(24.001085, -104.641488),new LatLng(24.000634, -104.640619),new LatLng(23.998919, -104.639366),
                        new LatLng(23.998203, -104.638894),new LatLng(23.997017, -104.638862),new LatLng(23.996596, -104.639184),new LatLng(23.989328, -104.618017)

                )
                .width(8)
                .color(Color.parseColor("#802E2EFE"));
        //.color(Color.BLUE);

        Polyline ruta = mGoogleMap.addPolyline(RutaVuelta);

    }


    @Override
    public void onPause() {
        super.onPause();
        //Unregister for location callbacks:
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }

    protected synchronized void buildGoogleApiClient() {
        Toast.makeText(this, "buildGoogleApiClient", Toast.LENGTH_SHORT).show();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle bundle) {
        Toast.makeText(this, "onConnected", Toast.LENGTH_SHORT).show();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            //place marker at current position

            latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("Posicion Actual");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            mCurrLocation = mGoogleMap.addMarker(markerOptions);
        }

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000); //5 seconds
        mLocationRequest.setFastestInterval(3000); //3 seconds
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLocationChanged(Location location) {

        //remove previous current location marker and add new one at current position
        if (mCurrLocation != null) {
            mCurrLocation.remove();
        }
        latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mCurrLocation = mGoogleMap.addMarker(markerOptions);

        //Toast.makeText(this,"Location Changed",Toast.LENGTH_SHORT).show();

        //If you only need one location, unregister the listener
        //LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        info= (TextView) findViewById(R.id.distancia);

        LatLng autobus=bus.getPosition();
        // LatLng markerLatLng = marker.getPosition();
        Location bus = new Location("");
        bus.setLatitude(autobus.latitude);
        bus.setLongitude(autobus.longitude);
        float distancia=mLastLocation.distanceTo(bus)/1000;

        info.setText("El camion se encuentra a "+distancia+" km aproximadamente de tu ubicacion");






    }




}

0 个答案:

没有答案