2017-01-23 43 views
0

我尽量让应用程序与谷歌地图工程。在我的地图我有currentmarker和markeroption,当我移动当前标记(蓝点),使招,但markeroption(红旗,与CurrenrtLocation标签)没得到与当前标记移动(蓝点)我。怎么可以markeroption与当前标记移动在googlemap.ThanksmarkerOption didnt与当前标记移动在谷歌地图

This picture shows when i move current marker move but markeroption didnt move

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mapbutton); 
    // 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);} 

@Override 

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

    mMap.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) { 
      buildGoogleApiClient(); 
      mMap.setMyLocationEnabled(true); 
     } 
    } 
    else { 
     buildGoogleApiClient(); 
     mMap.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(@Nullable 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 onLocationChanged(Location location) 
    { 
     if (location != null) { 
      // ---Get current location latitude, longitude--- 

      Log.d("LOCATION CHANGED", location.getLatitude() + ""); 
      Log.d("LOCATION CHANGED", location.getLongitude() + ""); 
      LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude()); 
      LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude()); 
      Marker currentLocationMarker = mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location")); 
      // Move the camera instantly to hamburg with a zoom of 15. 
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15)); 
      // Zoom in, animating the camera. 
      if (!zoomed) { 
       mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); 
       zoomed = true; 
      } 
      if (!firstPass){ 
       currentLocationMarker.remove(); 
      } 
      firstPass = false; 
      Toast.makeText(this,"Latitude = "+ 
          location.getLatitude() + "" +"Longitude = "+ location.getLongitude(), 
        Toast.LENGTH_LONG).show(); 

     } 
    } 



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

     // Asking user if explanation is needed 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 

      // Show an expanation 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. 

      //Prompt the user once explanation has been shown 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 


     } else { 
      // No explanation needed, we can request the permission. 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
     } 
     return false; 
    } else { 
     return true; 
    } 
} 
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. 
       if (ContextCompat.checkSelfPermission(this, 
         Manifest.permission.ACCESS_FINE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) { 

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

      } else { 

       // Permission denied, 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. 
     //You can add here other case statements according to your requirement. 
    } 
} 

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

}} 

回答

0

让currentLocationMarker类的属性,实际上是要删除它只是在添加它之后。

class YourActivity extends Activity implements onMapReady { 

    private Marker currentLocationMarker; 

    @Override 
    public void onLocationChanged(Location location){ 

     if (location != null){ 
      if (currentLocationMarker == null){ 
       currentLocationMarker = mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location")); 
      }else{ 
       currentLocationMarker.setPosition(new LatLng(location.getLatitude(), location.getLongitude())); 
      } 
     } 

    } 

} 

说明

在你的代码添加标记第一次的位置变化,那么你每次加位置改变一个新的,有立刻删除它:

Marker currentLocationMarker = mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location")); 
//... 
if (!firstPass){ 
    currentLocationMarker.remove(); 
} 

最好的办法是继续引用您创建的标记,而不是删除它创建一个新的,只是更新其位置,我在我的代码中做什么,如果标记不存在( currentLocationMarker == null)然后创建它并在类中保持引用。如果标记存在(else),那么只需更新其位置。

+0

你能解释一下吗? – farshad

+0

当然,看我的编辑 – Yoleth