6

我试图在Google地图的左上角显示一个TextView,告知用户它们距离特定标记点位置的距离是多少,即“您接近地点A”,但只在范围内显示TextView。如果他们移出范围,TextView将消失。如何在Google Maps android中始终检测当前位置附近的标记位置?

我知道Google Places API会向您显示附近的所有位置,但如果用户距离它们x距离,我只想在地图上显示附近位置的标记。

我该如何做到这一点?我仍然需要使用Google Places API吗?

我班的.java:

private GoogleMap mMap; 

private LatLng currLocation; 

private static final LatLng POINTA = new LatLng(32.820193, -117.232568); 
private static final LatLng POINTB = new LatLng(32.829129, -117.232204); 
private static final LatLng POINTC = new LatLng(32.821114, -117.231534); 
private static final LatLng POINTD = new LatLng(32.825157, -117.232003); 

// Array to store hotspot coordinates 
private ArrayList<LatLng> markerCoords = new ArrayList<LatLng>(); 
private static final int POINTS = 4; 

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

    setUpMapIfNeeded(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    // Create MenuInflater object to insert ActionBar buttons 
    MenuInflater inflater = getMenuInflater(); 

    // Display the ActionBar buttons from .xml 
    inflater.inflate(R.menu.menu_map, menu); 

    return true; 
} 

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

} 

private void setUpMapIfNeeded() 
{ 
    if (mMap == null) 
    { 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 

     // Check if we were successful in obtaining the map. 
     if (mMap != null) 
     { 
      markerCoords.add(POINTA); 
      markerCoords.add(POINTB); 
      markerCoords.add(POINTC); 
      markerCoords.add(POINTD); 

      setUpMap(); 
     } 
    } 
} 
private void setUpMap() 
{ 
    // Set My Location blue dot 
    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); 

    if (myLocation != null) 
    { 
     double latitude = myLocation.getLatitude(); 
     double longitude = myLocation.getLongitude(); 
     currLocation = new LatLng(latitude, longitude); 
    } 

    for (int i = 0; i < POINTS; i++) 
    { 
     mMap.addMarker(new MarkerOptions() 
       .position(markerCoords.get(i)) 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.post_it_marker))); 
    } 
} 

map.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <fragment 
     xmlns:map="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     map:cameraTargetLat="32.881416" 
     map:cameraTargetLng="-117.237428" 
     map:cameraZoom="15" 
     map:mapType="normal" /> 
</RelativeLayout> 

有人点我在正确的方向?由于

+0

嗨,你有没有设法解决这个问题,因为我面临着同样的挑战? – Zack

回答

8

简短的回答location.distanceTo(anotherLocation)

在setupMap我建议增加额外的监听器,以获得以简单的方式所有地点更改。

mMap.setMyLocationEnabled(true); 
mMap.setOnMyLocationChangeListener(this); 

然后在这个监听器,你把当前的位置和你的目标之间的距离。以米为单位的距离

@Override 
public void onMyLocationChange(Location location) { 
    Location target = new Location("target"); 
    for(LatLng point : new LatLng[]{POINTA, POINTB, POINTC, POINTD}) { 
     target.setLatitude(point.latitude); 
     target.setLongitude(point.longitude); 
     if(location.distanceTo(target) < METERS_100) { 
      // bingo! 
     } 
    } 
} 

当然,你应该让你的活动implements GoogleMap.OnMyLocationChangeListener

+0

我收到了一个错误,提到'this'陈述“setOnMyLocationChangeListener不能应用于此” – Pangu

+0

@Pangu加油,它很容易修复与android studio。只需让你的活动实现GoogleMap.OnMyLocationChangeListener即可。我更新了我的回答 –

+1

@pengrad ...我道歉我是新的android :(....我会试试这个,让你知道很快! – Pangu

相关问题