2016-04-28 8 views
0

Ich habe viele Markierungen in der Karte und habe bereits meinen aktuellen Standort-Service zur Verfügung gestellt, so einfach die Markierung ausblenden, wenn mein aktueller Standort darauf kommen und es zeigen wieder, wenn ich, lassen Sie bitte spezifisch, wenn Sie antworten, weil ich Anfänger bin „1.5.1 Android Studio mit“Hide Marker, wenn mein Standort darauf und zeigen Sie es wieder, wenn ich gehe

und dies ist mein Code, der in MapsActivity.java

package com.example.karim.trysomething; 

import android.content.Context; 
import android.content.IntentSender; 
import android.content.pm.PackageManager; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationManager; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.util.Log; 

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.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 



public class MapsActivity extends FragmentActivity implements 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     LocationListener { 

public static final String TAG = MapsActivity.class.getSimpleName(); 


private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

private GoogleMap mMap; 

private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 

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

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 




    mLocationRequest = LocationRequest.create() 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
      .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
      .setFastestInterval(1000); // 1 second, in milliseconds 


} 

@Override 
protected void onResume() { 
    super.onResume(); 
    setUpMapIfNeeded(); 
    mGoogleApiClient.connect(); 
} 

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

    if (mGoogleApiClient.isConnected()) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     mGoogleApiClient.disconnect(); 
    } 
} 


private void setUpMap() { 



    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) { 
     String[] permissions, 

     return; 
    } 
    mMap.setMyLocationEnabled(true); 


    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 


    Criteria criteria = new Criteria(); 


    String provider = locationManager.getBestProvider(criteria, true); 

    Location myLocation = locationManager.getLastKnownLocation(provider); 


    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 



    double latitude = myLocation.getLatitude(); 


    double longitude = myLocation.getLongitude(); 


    LatLng latLng = new LatLng(latitude, longitude); 


    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 


    mMap.animateCamera(CameraUpdateFactory.zoomTo(14)); 
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located")); 

    mMap.addMarker(new MarkerOptions() 
      .position(new LatLng(30.121683, 31.139405)) 
      .title("Location") 
      .snippet("ركنة المطحن")).showInfoWindow();  


} 



@Override 
public void onConnected(Bundle bundle) { 
    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; 
    } 
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location == null) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 

} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

    if (connectionResult.hasResolution()) { 
     try { 

      connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
     } catch (IntentSender.SendIntentException e) { 

      e.printStackTrace(); 
     } 
    } else { 
     Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
    } 
} 


@Override 
public void onLocationChanged(Location location) { 

} 
} 

Antwort

1

Vor Tauchgang in einen Code, Sie sollten die 'show'-Regel (Logik) definieren. Nehmen Sie einfach an, Sie befinden sich immer in einem Kreismittelpunkt mit einem Radius von 250 Metern (rosa Markierung). Wenn sich Ihr Standort geändert hat, vergleichen Sie jede Markerposition mit Ihrem Kreis. Verstecken Sie Marker, die sich im Kreis befinden, und zeigen Sie andere an.

enter image description hereenter image description here

Jedes Mal, wenn Sie Ihre Position geändert, rufen Sie eine Methode wie diese

// You have received new location and calling this method. 
private void filterMarkers(LatLng myLocation){ 

    double myLatitude = myLocation.latitude; 
    double myLongitude = myLocation.longitude; 

    float[] distance = new float[2]; 
    for(int m = 0; m < mMarkers.size(); m++){ 
     Marker marker = mMarkers.get(m); 
     LatLng position = marker.getPosition(); 
     double lat = position.latitude; 
     double lon = position.longitude; 

     /* distanceBetween method calculates the distance between two locations. 
      As i said above, you are in center of circle, means your location and 
      circle's location are same. 

      Also you could look distanceBetween method 
      http://developer.android.com/reference/android/location/Location.html#distanceBetween(double, double, double, double, float[]) 
     */ 
     Location.distanceBetween(lat, lon, myLatitude, 
       myLongitude, distance); 


     boolean inCircle = distance[0] <= mCircleRadius; 
     // If marker is in circle hide it, otherwise show. 
     marker.setVisible(!inCircle); 
    } 
} 
0

Dazu Sie 1.Compare aktuellen lat/long müssen durchgeführt werden, mit dem Marker lat/long, Tun Sie dies in Callback wie onMarkerRender und ändern Sie dann den aktuellen Marker. 2. Behalten Sie die aktuelle Markierung bei, ändern Sie später die Markierung, wenn Sie zur nächsten Markierung wechseln