2017-03-04 8 views
0

アプリでは、私はフラグメントクラスで現在の位置を取得していますが、アンドロイドスタジオの他の場所に移動すると変更されません。車のようにマーカーを移動する方法は、フラグメントクラスの現在の位置のアンドロイドマップに行く

  1. 私のgooglemap(マーカー)の部分に車のように動いている現在のアプリケーションを作成したいと思います。

  2. onLocationChangedのメソッドとは何ですか?

  3. インターポレータとは何ですか?
+0

だけ –

+0

使用場所がリスナーを変更する方法と、更新マーカ位置をonLocationChange使用-https://developer.android.com/reference/android/location/LocationListener.html –

+0

は、これはあなたを助けるかもしれない - のhttp:/ /stackoverflow.com/a/41828471/3425390 –

答えて

0
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, 
    com.google.android.gms.location.LocationListener { 

Location location; 
ImageView adddatanotfoundimage; 
LinearLayout walletsnackbar; 
Context mcontext; 
TextView totbal; 
List<String> noparkingarrayid; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
private GoogleMap mGoogleMap; 

String lat1, lat2, long1, long2; 
MarkerOptions markerOptions; 
Marker marker; 
String address; 
static String Location1 = ""; 
static String Location2 = ""; 

protected LatLng mCenterLocation = new LatLng(26.9598505, 75.77874); 
final LatLng SomePos = new LatLng(12.7796354, 77.4159606); 

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

    mcontext = MainActivity.this; 

    try { 
     if (mGoogleMap == null) { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
       mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
      } 
     } 
     mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      return; 
     } 

     mGoogleMap.getUiSettings().setMapToolbarEnabled(false); 
     mGoogleMap.setMyLocationEnabled(true); 
     // mGoogleMap.setTrafficEnabled(false); 
     // mGoogleMap.setIndoorEnabled(false); 
     // mGoogleMap.setBuildingsEnabled(true); 
     mGoogleMap.getUiSettings().setZoomControlsEnabled(false); 
     mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(SomePos)); 
     mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder() 
       .target(mGoogleMap.getCameraPosition().target) 
       .zoom(17) 
       .bearing(30) 
       .tilt(45) 
       .build())); 

     marker = mGoogleMap.addMarker(new MarkerOptions() 
       .position(SomePos) 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.car)) 
       .title("Hello world")); 


     mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { 
      @Override 
      public boolean onMarkerClick(Marker arg0) { 

       final LatLng startPosition = marker.getPosition(); 
       final LatLng finalPosition = new LatLng(12.7801569, 77.4148528); 
       final Handler handler = new Handler(); 
       final long start = SystemClock.uptimeMillis(); 
       final Interpolator interpolator = new AccelerateDecelerateInterpolator(); 
       final float durationInMs = 3000; 
       final boolean hideMarker = false; 
       bearingBetweenLocations(SomePos,finalPosition); 

       handler.post(new Runnable() { 
        long elapsed; 
        float t; 
        float v; 

        @Override 
        public void run() { 
         // Calculate progress using interpolator 
         elapsed = SystemClock.uptimeMillis() - start; 
         t = elapsed/durationInMs; 

         LatLng currentPosition = new LatLng(
           startPosition.latitude * (1 - t) + finalPosition.latitude * t, 
           startPosition.longitude * (1 - t) + finalPosition.longitude * t); 

         marker.setPosition(currentPosition); 

         // Repeat till progress is complete. 
         if (t < 1) { 
          // Post again 16ms later. 
          handler.postDelayed(this, 16); 
         } else { 
          if (hideMarker) { 
           marker.setVisible(false); 
          } else { 
           marker.setVisible(true); 
          } 
         } 
        } 
       }); 

       return true; 

      } 

     }); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    } 
    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
     finish(); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 

     requestLocationUpdates(); 
    } 

    public void requestLocationUpdates() { 
     if (ActivityCompat.checkSelfPermission(mcontext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mcontext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      return; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (location != null) { 

      Log.e("LatitudeMap", "" +location.getLatitude()); 
      Log.e("LogtitudeMap",""+location.getLongitude()); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onLocationChanged(Location location) { 


    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 
+0

私はApiを使用しています25投稿できないシンボル –