2016-06-15 11 views
-1

Ich habe eine Funktion geoLocate(), die einen vom Benutzer eingegebenen Ort findet. Ich möchte, dass diese Funktion einen Ort in der Nähe des aktuellen Standorts des Benutzers findet. Kann jemand helfen?In der Nähe befindliche Orte finden

Wenn ich geoLocate("McDonald's") anrufen, möchte ich, dass es den nächsten McDonald's relativ zum Standort des Benutzers findet.

public void geoLocate(String searchString) throws IOException { 
    Geocoder gc = new Geocoder(this); 
    List<Address> list = gc.getFromLocationName(searchString, 3); 

    if (list.size() > 0) { 
     android.location.Address add = list.get(0); 
     String locality = add.getLocality(); 
     Toast.makeText(this, "Found: " + locality, Toast.LENGTH_SHORT).show(); 

     double lat = add.getLatitude(); 
     double lng = add.getLongitude(); 
     gotoLocation(lat, lng, 17); 

     if (marker != null) { 
      marker.remove(); 
     } 
     MarkerOptions options = new MarkerOptions().title(locality).position(new LatLng(lat, lng)); 
     marker = mMap.addMarker(options); 

    } else { 
     Toast.makeText(this, "No results found for: " + searchString, Toast.LENGTH_LONG).show(); 
    } 
} 

Finden aktuellen Standort als Referenz:

public void setCurrentLocation() { 
    try { 
     Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(mLocationClient); 
     if (currentLocation == null) { 
      Toast.makeText(this, "Couldn't connect!", Toast.LENGTH_SHORT).show(); 
     } else { 
      LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()); 
      CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, 15); 
      mMap.animateCamera(update); 

      if (myLocationMarker != null) { 
       myLocationMarker.remove(); 
      } 
      MarkerOptions options = new MarkerOptions().title(currentLocation.getLatitude() + 
        ", " + currentLocation.getLongitude()).position(latLng); 
      myLocationMarker = mMap.addMarker(options); 
     } 
    } catch (SecurityException e) { 
     e.printStackTrace(); 
     Toast.makeText(this, "My Location not enabled!", Toast.LENGTH_SHORT).show(); 
    } 
} 

Antwort

1

Sobald Sie Ihren aktuellen Standort (Breite, Länge) abgerufen haben, können Sie die nearbysearch des Places API Web Service verwenden für McDonnald ist wie folgt zu suchen: https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=lat,lon&radius=500&type=restaurant&name=mcdonalds&key=YOUR_API_KEY . Sie können diese URL tatsächlich bauen eine StringBuilder wie folgt aus:

StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"); 
googlePlacesUrl.append("location=" + latitude + "," + longitude); 
googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS); 
googlePlacesUrl.append("&types=" + type); 
googlePlacesUrl.append("&name=mcdonalds"); 
googlePlacesUrl.append("&key=" + YOUR_GOOGLE_API_KEY); 

Sie dann machen eine HTTP Anfrage an die URL Ihrer HTTP-Client der Wahl verwenden und verarbeiten dann das Ergebnis (JSON). Dadurch werden Orte zurückgegeben, die zu Ihrer Suche passen. Außerdem wird die Umgebung jedes dieser Orte angezeigt.

Ich hoffe, dies gibt Ihnen einige Ideen, wie Sie vorgehen. Bitte versuchen Sie es und lassen Sie es mich wissen, wenn es hilft.

Verwandte Themen