2017-04-11 3 views
3

私は継続的に動いている人の位置を表示する別のマーカーを入れたいと思う。私は人がGoogleマップ上で動くようにマーカーを更新したい、私はhttps://stackoverflow.com/a/33885379/5324829リンクから試してみましたが、私はあらかじめ定義された位置にマーカーをアニメーションすることができません、誰も私はこれを解決するために私を助けることができますか?事前のおかげで、ここに私のコードは、後であなたがあなたのバックグラウンドサービスにそれを置くことができ、場所の変更リスナーからコードを削除し、ちょうど目的を確認するためにどこかにボタンのようにそれを置く、としてそれを使用することができます。..位置に基づいてアンドロイドのGoogleマップでマーカーを移動する方法は、apiから来る

GoogleMap mGoogleMap; 
    SupportMapFragment mapFrag; 
    LocationRequest mLocationRequest; 
    GoogleApiClient mGoogleApiClient; 
    Location mLastLocation; 
    Marker mCurrLocationMarker; 
    boolean isFirstTime = true; 
    private ArrayList<LatLng> markerPoints; 

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

//  getSupportActionBar().setTitle("Map Location Activity"); 
     //check for internet 

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

     markerPoints = new ArrayList<LatLng>(); 


     //call web service to fetch location 
     //on response call direction api 
     // 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 

     //stop location updates when Activity is no longer active 
     if (mGoogleApiClient != null) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) 
    { 
     mGoogleMap=googleMap; 
     mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 



     //Initialize Google Play Services 
     if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.ACCESS_FINE_LOCATION) 
        == PackageManager.PERMISSION_GRANTED) { 
       //Location Permission already granted 
       buildGoogleApiClient(); 
       mGoogleMap.setMyLocationEnabled(true); 

       mGoogleMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() { 
        @Override 
        public boolean onMyLocationButtonClick() { 
         LatLng latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()); 
         CameraUpdate center = 
           CameraUpdateFactory.newLatLng(latLng); 
         CameraUpdate zoom = CameraUpdateFactory.zoomTo(15.0f); 

         mGoogleMap.moveCamera(center); 
         mGoogleMap.animateCamera(zoom); 
         return false; 
        } 
       }); 

      } else { 
       //Request Location Permission 
       checkLocationPermission(); 
      } 
     } 
     else { 
      buildGoogleApiClient(); 
      mGoogleMap.setMyLocationEnabled(true); 
     } 
    } 

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

    @Override 
    public void onConnected(Bundle bundle) { 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(1000); 
     mLocationRequest.setFastestInterval(1000); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) {} 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) {} 

    @Override 
    public void onLocationChanged(Location location) 
    { 
     mLastLocation = location; 
     if (mCurrLocationMarker != null) { 
      mCurrLocationMarker.remove(); 
     } 

     /*this condition is to set the map to the current location only for the first time, 
     after onLocationChanged() method is called every second so it will move us to current location every second*/ 
     if (isFirstTime) { 
      //Place current location marker 
      LatLng 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)); 
      mCurrLocationMarker = mGoogleMap.addMarker(markerOptions); 
      */ 

      CameraUpdate center = 
        CameraUpdateFactory.newLatLng(latLng); 
      CameraUpdate zoom = CameraUpdateFactory.zoomTo(16.0f); 

      mGoogleMap.moveCamera(center); 
      mGoogleMap.animateCamera(zoom); 
      //move map camera 
      // mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,15.0f)); 
      isFirstTime = false; 
      markerPoints.add(new LatLng(23.0515539,72.5158113)); 
      markerPoints.add(new LatLng(23.0504289,72.5153713)); 
      markerPoints.add(new LatLng(23.0491435,72.5159617)); 
      markerPoints.add(new LatLng(23.0482305,72.5155917)); 
      markerPoints.add(new LatLng(23.0473917,72.5154952)); 

      Bitmap Icon = BitmapFactory.decodeResource(getResources(), R.mipmap.bus); 
      setAnimation(mGoogleMap,markerPoints,Icon); 
     } 


    } 

    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 
    private void checkLocationPermission() { 
     if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 

      // Should we show an explanation? 
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
        Manifest.permission.ACCESS_FINE_LOCATION)) { 

       // Show an explanation to the user *asynchronously* -- don't block 
       // this thread waiting for the user's response! After the user 
       // sees the explanation, try again to request the permission. 
       new AlertDialog.Builder(this) 
         .setTitle("Location Permission Needed") 
         .setMessage("This app needs the Location permission, please accept to use location functionality") 
         .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialogInterface, int i) { 
           //Prompt the user once explanation has been shown 
           ActivityCompat.requestPermissions(MainActivity.this, 
             new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
             MY_PERMISSIONS_REQUEST_LOCATION); 
          } 
         }) 
         .create() 
         .show(); 


      } else { 
       // No explanation needed, we can request the permission. 
       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
         MY_PERMISSIONS_REQUEST_LOCATION); 
      } 
     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, 
              String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case MY_PERMISSIONS_REQUEST_LOCATION: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        // permission was granted, yay! Do the 
        // location-related task you need to do. 
        if (ContextCompat.checkSelfPermission(this, 
          Manifest.permission.ACCESS_FINE_LOCATION) 
          == PackageManager.PERMISSION_GRANTED) { 

         if (mGoogleApiClient == null) { 
          buildGoogleApiClient(); 
         } 
         mGoogleMap.setMyLocationEnabled(true); 
        } 

       } else { 

        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
        Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show(); 
       } 
       return; 
      } 

      // other 'case' lines to check for other 
      // permissions this app might request 
     } 
    } 



    public static void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint, final Bitmap bitmap) { 


     Marker marker = myMap.addMarker(new MarkerOptions() 
       .icon(BitmapDescriptorFactory.fromBitmap(bitmap)) 
       .position(directionPoint.get(0)) 
       .flat(true)); 

     myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 16)); 

     animateMarker(myMap, marker, directionPoint, false); 
    } 


    private static void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint, 
             final boolean hideMarker) { 
     final Handler handler = new Handler(); 
     final long start = SystemClock.uptimeMillis(); 
     Projection proj = myMap.getProjection(); 
     final long duration = 30000; 

     final Interpolator interpolator = new LinearInterpolator(); 

     handler.post(new Runnable() { 
      int i = 0; 

      @Override 
      public void run() { 
       long elapsed = SystemClock.uptimeMillis() - start; 
       float t = interpolator.getInterpolation((float) elapsed 
         /duration); 
       if (i < directionPoint.size()) 
        marker.setPosition(directionPoint.get(i)); 
       i++; 


       if (t < 1.0) { 
        // Post again 16ms later. 
        handler.postDelayed(this, 5); 
       } else { 
        if (hideMarker) { 
         marker.setVisible(false); 
        } else { 
         marker.setVisible(true); 
        } 
       } 
      } 
     }); 
    } 
+0

あなたの問題についてもう少し詳しく説明できますか?それはあなたが何をしたいのかわかりません –

+0

私はタクシーアプリで乗客に移動すると、私の位置マーカー位置に他のマーカーをアニメートしたいです。 –

答えて

1

です。

btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       markerPoints.clear(); 

       markerPoints.add(new LatLng(23.049543, 72.517195)); 
       markerPoints.add(new LatLng(23.048457, 72.516787)); 
       markerPoints.add(new LatLng(23.048989, 72.516973)); 
       markerPoints.add(new LatLng(23.048263, 72.516667)); 
       markerPoints.add(new LatLng(23.047409, 72.516281)); 
       markerPoints.add(new LatLng(23.046219, 72.515696)); 

       Bitmap Icon = BitmapFactory.decodeResource(getResources(), R.mipmap.bus); 
       setAnimation(mGoogleMap,markerPoints,Icon); 
      } 
     }); 


public static void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint, final Bitmap bitmap) { 

     myMap.clear(); 
     Marker marker = myMap.addMarker(new MarkerOptions() 
       .icon(BitmapDescriptorFactory.fromBitmap(bitmap)) 
       .position(directionPoint.get(0)) 
       .flat(true)); 

     myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 16)); 

     animateMarker(myMap, marker, directionPoint, false); 
    } 


    private static void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint, 
             final boolean hideMarker) { 
     final Handler handler = new Handler(); 
     final long start = SystemClock.uptimeMillis(); 
     Projection proj = myMap.getProjection(); 
     final long duration = 30000; 

     final Interpolator interpolator = new LinearInterpolator(); 

     handler.post(new Runnable() { 
      int i = 0; 

      @Override 
      public void run() { 
       long elapsed = SystemClock.uptimeMillis() - start; 
       float t = interpolator.getInterpolation((float) elapsed 
         /duration); 
       if (i < directionPoint.size()) 
        marker.setPosition(directionPoint.get(i)); 
       i++; 


       if (t < 1.0) { 
        // Post again 16ms later. 
        handler.postDelayed(this, 150); 
       } else { 
        if (hideMarker) { 
         marker.setVisible(false); 
        } else { 
         marker.setVisible(true); 
        } 
       } 
      } 
     }); 
    } 
関連する問題