【发布时间】:2016-01-07 05:33:30
【问题描述】:
我正在开发一个具有多种标记的 android 应用程序。
这是我的MapsActivity.java 文件。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
GoogleMap googleMap;
List<MapLocation> restaurantList;
List<MapLocation> hotelList;
List<Marker> restaurantMarkers = new ArrayList<>();
List<Marker> hotelMarkers = new ArrayList<>();
MapLocation r1 = new MapLocation(6.9192,79.8950, "Mnhatten Fish Market" );
MapLocation r2 = new MapLocation(6.9017,79.9192, "Dinemore" );
MapLocation r3 = new MapLocation(6.9147,79.8778, "KFC" );
MapLocation r4 = new MapLocation(6.9036,79.9547, "McDonalds" );
MapLocation r5 = new MapLocation(6.8397,79.8758, "Dominos" );
MapLocation h1 = new MapLocation(6.9006,79.8533, "Hilton" );
MapLocation h2 = new MapLocation(6.8889,79.8567, "Galadari" );
MapLocation h3 = new MapLocation(6.8756,79.8608, "Hotel Lagoon Dining" );
MapLocation h4 = new MapLocation(6.7991,79.8767, "Aqua Pearl Lake Resort " );
MapLocation h5 = new MapLocation(6.5833,79.1667, "KZ Resort" );
//Buttons
private final LatLng LOCATION_COLOMBO = new LatLng(6.9270786,79.861243);
private final LatLng LOCATION_GALLE = new LatLng(6.0334009,80.218384);
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
restaurantList = new ArrayList<>();
hotelList = new ArrayList<>();
restaurantList.add(r1);
restaurantList.add(r2);
restaurantList.add(r3);
restaurantList.add(r4);
restaurantList.add(r5);
hotelList.add(h1);
hotelList.add(h2);
hotelList.add(h3);
hotelList.add(h4);
hotelList.add(h5);
// Give text to buttons
Button buttonloc1 = (Button)findViewById(R.id.btnLoc1);
buttonloc1.setText("Colombo");
Button buttonloc2 = (Button)findViewById(R.id.btnLoc2);
buttonloc2.setText("Galle");
Button buttoncity = (Button)findViewById(R.id.btnCity);
buttoncity.setText("My Location");
Button buttonremove = (Button) findViewById(R.id.removeMarker);
buttonremove.setText("Remove");
CheckBox checkRestaurants = (CheckBox) findViewById(R.id.checkRestaurants);
checkRestaurants.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
showRestaurants();
} else {
hideRestaurants();
}
}
});
CheckBox checkHotels = (CheckBox) findViewById(R.id.checkHotels);
checkHotels.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
showHotels();
} else {
hideHotels();
}
}
});
// Marker to Dinemore
mMap.addMarker(new MarkerOptions()
.position(LOCATION_COLOMBO)
.title("I'm in Colombo :D")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
);
// Marker to Barista
mMap.addMarker(new MarkerOptions()
.position(LOCATION_GALLE)
.title("I'm in Galle :D")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
);
//When touch again on the map marker title will hide
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
}
});
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMyLocationChange(Location location) {
Location target = new Location("target");
for(LatLng point : new LatLng[]{}) {
target.setLatitude(point.latitude);
target.setLongitude(point.longitude);
if(location.distanceTo(target) < 100) {
// bingo!
}
}
}
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
setUpMap();
}
public void showRestaurants() {
restaurantMarkers.clear();
for (MapLocation loc : restaurantList){
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(loc.lat, loc.lon))
.title(loc.title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
restaurantMarkers.add(marker);
}
}
public void showHotels() {
hotelMarkers.clear();
for (MapLocation loc : hotelList){
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(loc.lat, loc.lon))
.title(loc.title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
hotelMarkers.add(marker);
}
}
public void hideRestaurants(){
for (Marker marker : restaurantMarkers){
marker.remove();
}
}
public void hideHotels(){
for (Marker marker : hotelMarkers){
marker.remove();
}
}
public void onClick_City(View v){
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
// When click on this button, Map shows the place of Dinemore
public void onClick_Loc1(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_COLOMBO,10);
mMap.animateCamera(update);
}
// When click on this button, Map shows the place of Barista
public void onClick_Loc2(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_GALLE,10);
mMap.animateCamera(update);
}
// To rmove All the Markers
public void onClick_Remove(View v){
mMap.clear();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
public class MapLocation {
public MapLocation(double lt, double ln, String t){
lat = lt;
lon = ln;
title = t;
}
public double lat;
public double lon;
public String title;
}
}
现在,我需要做的是,我只需要显示设备附近的标记。
这是我需要做的步骤。
标识设备区域(500m)的一个圆形
识别此圆圈内的标记
显示已识别的标记
在设备移动时重新识别圆圈
设备行驶时需要更换圆圈和标记。
我可以在 Android 中执行此操作吗?如果可以请帮助我这样做或发布可能对我有帮助的文章。
提前致谢。
【问题讨论】:
-
@user5259714 你好,我也面临同样的挑战,如果可能的话,请在这里帮助我:stackoverflow.com/questions/47351094/…
标签: android google-maps