在Android中显示设备附近的标记

时间:2015-10-09 10:03:00

标签: android google-maps

我正在开发一款具有多种标记的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. 识别设备区域的圆圈(500米)

  2. 识别此圈内的标记

  3. 显示已识别的标记

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

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

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

    提前致谢。

4 个答案:

答案 0 :(得分:0)

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

在我之前的项目中,我正在从100英里范围内的服务器n位置向下同步。你知道,这确保你只获得你需要的东西,而不是拥有你不需要的很多地方。

所以,我同步了15个附近位置并将标记添加到地图中。然后当用户移动时,我再次同步以确保我只能到达附近的商家位置。这对我没有任何问题。

我希望这会对你有所帮助;所以简而言之:

  1. 假设您在服务器中提前知道位置(远程),则向下同步n个位置 - 您可以使用SQL查询(语句)获取最近的位置。为此,您需要为每个位置提供纬度和经度。
  2. 完成同步后,只需循环浏览数据并根据位置的纬度和经度设置标记!
  3. 这就是你真正需要的!

    祝你好运!

答案 1 :(得分:0)

当然可以这样做。

您可以通过标记None检查设备的距离。在这里,您还可以重新识别圈子。

答案 2 :(得分:0)

好吧,我可以用两个链接来帮助你。

首先,我将接受您知道如何从地图获取您的位置。之后我们将按照本指南进行制作。

How to replace a circle

之后,对于半径,我可以通过链接向您显示一个方法,以便您可以关注它并自行修改

Detect nearby places

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

修改

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

表示r1:

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

    <!-- US Beta Target -->
    <target name="test-integration-assert-beta">
       <test-environment country="US" stage="Beta" host.name="URL Goes Here" emailid="" password="" company="" invalidpassword="" materialset=""/>
           <echo message="integTest.failure = ${integTest.failure}" />
           <echo message="failedTests = ${failedTests}" />
           <condition property="failedTests">
               <and>
                   <istrue value="${integTest.failure}" />
                   <available file="${integ.test.dir}/testng-failed.xml" />
               </and>
           </condition>
           <antcall target="test-integration-assert-beta-rerun">
           </antcall>
    </target>   

    <!-- US Beta Target (Re-run) -->
        <target name="test-integration-assert-beta-rerun" description="Rerunning Failed Beta Tests" if="failedTests">
           <echo message="Running Failed Integration Tests..." />
           <echo message="rerunFailedTests.failure = ${rerunFailedTests.failure}" />
           <copy file="${output.dir}/brazil-integ-tests/testng-failed.xml"
              tofile="${output.dir}/testng-failed.xml" />
       <test-environment country="US" stage="Beta" host.name="URL Goes Here" emailid="" password="" company="" invalidpassword="" materialset=""/>
           <echo message="rerunFailedTests.failure = ${rerunFailedTests.failure}" />
           <fail message="Tests Failed on rerun">
               <condition>
                   <istrue value="${rerunFailedTests.failure}" />
               </condition>
           </fail>
        </target>

答案 3 :(得分:0)