【问题标题】:Show markers near the device in Android在 Android 设备附近显示标记
【发布时间】: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;
}

}

现在,我需要做的是,我只需要显示设备附近的标记。

这是我需要做的步骤。

  1. 标识设备区域(500m)的一个圆形

  2. 识别此圆圈内的标记

  3. 显示已识别的标记

  4. 在设备移动时重新识别圆圈

  5. 设备行驶时需要更换圆圈和标记。

我可以在 Android 中执行此操作吗?如果可以请帮助我这样做或发布可能对我有帮助的文章。

提前致谢。

【问题讨论】:

标签: android google-maps


【解决方案1】:

好的,我可以通过两个链接帮助您。

首先我会接受您知道如何从地图中获取您的位置。然后按照本指南绘制圆圈

How to replace a circle

在那之后,对于半径,我可以通过链接向您展示一个方法,以便您可以按照它并自己修改它

Detect nearby places

所以按照这个,你会得到你的答案。

编辑

将您的位置更改为 latlng,如下所示;

对于 r1:

LatLng POINTA = new LatLng(6.9192,79.8950); //对其他人做同样的事情并将其放入数组belove。它很简单,请不要指望直接回答并为它工作。

for(LatLng point : new LatLng[]{POINTA, POINTB, POINTC, POINTD}) {
        target.setLatitude(point.latitude);
        target.setLongitude(point.longitude);
        if(location.distanceTo(target) < METERS_100) {
            // bingo!
        }
    }

【讨论】:

  • 嗨,我尝试了stackoverflow.com/questions/32284708/… 中的示例,但 METERS_100 给了我一个错误。你知道为什么吗?
  • 也许他有一个仪表的模型类。您是否尝试过将 100 写为 int 而不是它?
  • 100 件作品。你能看看我不能像他的代码一样将我的 r1,r2,r3,r4,r5,h1,h2,h3,h4 和 h5 值传递到 for 循环中的代码。
  • 真的很抱歉提出愚蠢的问题。问题解决了。现在我可以在我的区域内加载标记。谢谢:)
【解决方案2】:

这里最好的方法是简单地限制您需要标记可见的距离:

在我之前的项目中,例如,我从服务器同步 100 英里内的 n 个位置。你看,这样可以确保你只得到你需要的东西,而不是有很多你不需要的地方。

因此,我同步了 15 个附近的位置并将标记添加到地图中。然后,当用户移动时,我会再次同步以确保只获得附近的营业地点。这对我来说没有任何问题。

希望对您有所帮助;简而言之:

  1. 假设您提前知道服务器中的位置(远程),则同步 n 个位置 - 您可以使用 SQL 查询(语句)获取最近的位置。为此,您需要每个位置的纬度和经度。
  2. 同步完成后,只需遍历数据并设置标记 - 基于位置的纬度和经度!

这就是你真正需要的!

祝你好运!

【讨论】:

  • 你能在我的代码中展示如何做到这一点吗?我是安卓新手。 :(
【解决方案3】:

你当然可以做到。

您可以检查设备与标记 onLocationChanged 的距离。在这里您还可以重新识别圈子。

【讨论】:

  • 确保这有助于识别我所在位置圈内的标记。你能在我的代码中展示如何吗?谢谢。
【解决方案4】:
猜你喜欢
  • 2020-02-24
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 2018-03-22
  • 2020-01-24
  • 2016-12-13
  • 1970-01-01
相关资源
最近更新 更多