【问题标题】:How to search near by places from marker's location?如何从标记位置搜索附近的地方?
【发布时间】:2016-12-28 06:24:16
【问题描述】:

我的位置会根据另一个活动提供的意图发生变化,它可以正常工作,显示我想要来自 LatitudeDegreeLongitudeDegree 的位置。现在我想在用户选择附近的地方显示它。比如如果他键入 ATM,那么应该显示附近的 ATM 等等。

这是我获取位置的代码,我尝试使用地点选择器,但我不知道我是否正确以及我应该如何使用。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{

    private GoogleMap mMap;
    private String mAirportName;
    private int mLatitudeDegree;
    private int mLongitudeDegree;
    private int PLACE_PICKER_REQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        Intent newIntent = getIntent();
        mAirportName = newIntent.getStringExtra("airportName");
        mLatitudeDegree = newIntent.getIntExtra("latDegree", 200);
        mLongitudeDegree = newIntent.getIntExtra("lngDegree", 201);

        /*PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

        try {
            startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }*/
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker and move the camera
        LatLng latLng = new LatLng(mLatitudeDegree, mLongitudeDegree);
        mMap.addMarker(new MarkerOptions().position(latLng).title(mAirportName));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(5));
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {
                Place place = PlacePicker.getPlace(data, this);
                String toastMsg = String.format("Place: %s", place.getName());
                Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
            }
        }
    }
}

【问题讨论】:

  • 你有那些附近物品的纬度吗?
  • LatitudeDegree 和 LongitudeDegree 只不过是我的 lat lng
  • 如果您想显示其他位置,例如 ATM,您还需要那些 lat lng 的位置
  • lat lng 适用于我必须从我的要求中搜索的那个城市位置,然后在获得该位置后,我必须搜索它附近的地方
  • 我没有地方 lat lng

标签: android google-maps google-places-api


【解决方案1】:

如果您有 lat lang 的附近项目,则使用 Marker 并标记它们。如果没有,则需要购买此类 API 或创建自己的 数据库 或尝试搜索是否有开放的 API 可用.

【讨论】:

    【解决方案2】:

    如果您有其他地方(例如:纺织店)的位置经纬度,您可以使用此方法检查用户与位置之间的距离;

     public double callDistanceToMethod(LatLng latLngStartPosition, LatLng latLngEndPosition) {
    
            Location endLocation = new Location("EndPosition");
            endLocation.setLatitude(latLngEndPosition.latitude);
            endLocation.setLongitude(latLngEndPosition.longitude);
    
            Location startLocation = new Location("StartPosition");
            startLocation.setLatitude(latLngStartPosition.latitude);
            startLocation.setLongitude(latLngStartPosition.longitude);
    
    
            double distance = startLocation.distanceTo(endLocation);
            System.out.println("Hello Distance**--->" + distance);
            return distance;
        }
    

    像下面这样调用这个方法(将以米为单位返回)

    Double distanceBetweenTwoLocations = callDistanceToMethod(startCoordinatesInLatLng, endCoordinatesInLatLng);
    

    如果你现在想要,你可以使用 if 条件

    if(distanceBetweenTwoLocations <100){
    // ok user is almost there in 100 meter range
    // display an alert , send a push notification or anything 
    
    
    }
    

    【讨论】:

      【解决方案3】:

      除了上面的答案:如果您将附近的地点保存在列表或数据库中,您可以通过以下算法找到最近的地点:

      distancefaktor = ((currentlat-listitem_lat)*(currentlat-listitem_lat)+(currentlon-listitem_lon)*(currentlon*listitem_lon))
      

      然后按 distancefaktor 对列表进行排序并取 i.E.前 10 项。

      我很晚才看到,你正在研究谷歌地图。在这种情况下,您必须使用 google api 来获取附近的地点:

      https://developers.google.com/places/web-service/search?hl=de

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-31
        • 1970-01-01
        • 1970-01-01
        • 2016-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多