2016-12-28 36 views
0

我的位置根据其他活动提供的意图而改变,它工作正常,显示我想从LatitudeDegreeLongitudeDegree获取的位置。现在我想显示其附近的地方用户choice.like如果他键入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(); 
      } 
     } 
    } 
} 
+0

你有那些项目附近的纬度经度? –

+0

LatitudeDegree和LongitudeDegree不过是我的经纬度 –

+0

如果你想显示像ATM这样的其他位置,你还需要那些经纬度 –

回答

0

如果你有LAT郎附近的项目然后用标记并将其标记。如果您没有,那么您需要购买这样的API或创建您自己的数据库或尝试搜索是否有开放API可用。

0

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

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(distanceBetweenTwoLocations <100){ 
// ok user is almost there in 100 meter range 
// display an alert , send a push notification or anything 


} 
+0

好的,谢谢你:-) –

+0

如果有帮助,你可以接受/投票 –

0

除了上面的回答:如果你可以将你附近的地方放在一个列表或数据库中,通过以下算法可以找到最近的地方:

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

然后通过distancefaktor对列表进行排序并采取i.E.前10个项目。

我看到很晚了,你正在研究谷歌地图。在你必须使用谷歌的API,这种情况下,取得附近的地方:

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