I am implementing the search functionality on the google maps. For this I am using the Places.GeoDataApi of google.
But the problem I am facing that it is searching based on the LatLanBounds value.
Currently I am passing sydney specific bounds in below api,so at the time of search its biased towards these bounds and checking syndey specific locations first.
private static final LatLngBounds mBounds= new LatLngBounds(
new LatLng(-34.041458, 150.790100), new LatLng(-33.682247, 151.383362));
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, mPlaceFilter);
As per my requirement, Above API should return the result of near by area first. For example, if user is in India and search for something it should return the suggestions related to India on priority and If user in Brazil try to search something API should return the suggestion related to Brazil. I do not know how to pass the dynamic LatLng bounds values based on user current country or location.
your help will be really appreciated.
This is month old question, but I hope my answer will help the OP or somebody else with similar problems.
The answer for main question is pretty straighforward:
Create new LatLngBounds (docs) object each time you get information about user location and pass this object as a parameter of .getAutocompletePredictions:
LatLng sw = new LatLng(SOUTH_WEST_LAT, SOUTH_WEST_LNG);
LatLng ne = new LatLng(NORTH_EAST_LAT, NORTH_EAST_LNG);
LatLngBounds bounds = new LatLngBounds(sw, ne);
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, constraint, bounds, null);
These two points, south-west and north-east are opposite corners of the square that makes an area around user's location. This is the bound.
About your second question, in the comment. I suppose you have your user location (eg. from GPS), for example in Brazil, and you need to find the bounds of this location.
To do that, I would use basic Google Places API call (you can try it out in your browser):
https://maps.googleapis.com/maps/api/geocode/json?address=Brazil
that will return you JSON formatted data about Brazil with field bounds in it:
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 42.1282269,
"lng" : -87.7108162
},
"southwest" : {
"lat" : 42.0886089,
"lng" : -87.7708629
}
},
...
}
As you can see, these are the north-east and south-west bounds of square in which Brazil lies. You can put them into the LatLngBounds object now.
Related
As the title states I want to know how can I find Intersecting point of two different routes in google maps API.
please view this image for better visualisation:
You can use, for example, PolyUtil.isLocationOnPath(LatLng point, java.util.List<LatLng> polyline, boolean geodesic, double tolerance) from Google Maps Android API Utility Library. In this case you need to iterate over each point of the "red" polyline and check (with isLocationOnPath()) if it intersects (lays on) each segment of the "blue" polyline. Something like that:
for (LatLng point : redPolyline.getPoints()) {
if (PolyUtil.isLocationOnPath(point, bluePolyline.getPoints(), true, 50)) {
// now "point" - is your target point
pointWhatYouWantToGet = point;
...
}
}
where 50 - is tolerance (in meters).
If you need polylines segments intersection, take a look at this answer of antonio.
Is there a way to color only a part of the route in a different color?
Like drawing a polyline but one that takes the road configuration into consideration.
No, that is not supported. But if you are wondering about changing only the traveled portion color I previously answered here.
You can extract the route geometry to get the list of GeoCoordinates constituing the polyline:
geoCoordinates = mapRoute.getRoute().getRouteGeometry()
Then you can draw portions of the route in various colors by building new polylines from these coordinates:
GeoPolyline geoPolyline = new GeoPolyline();
for (GeoCoordinates point : geoCoordinates) {
if (<your condition>){
geoPolyline.add(point);
}
}
MapPolyline mapPolyline = new MapPolyline(geoPolyline);
mapPolyline.setLineColor(<color>);
map.addMapObject(mapPolyline)
I'm trying to use google maps and need to get natural feature like ocean, sea, etc. All i need is just names but all i get is ZERO_RESULTS. My request is like http://maps.googleapis.com/maps/api/geocode/json?latlng=0,0&sensor=true_or_false . Is it possible to get natural features with lat/lon ? Please turn me in right direction
You can use Place API with type
var request = {
location : {
lat : 0,
lng : 0
},
radius : radius,
types: ['natural_feature']
};
I would like to click a spot on a Google maps v2 android map. If the clicked point intersects a point on a polyline path, then display the polyline. I do not see any documented clickable events for polylines in android. I tried to extend the current Polyline object (marked final)
What other options do I have?
You can use library:
https://github.com/googlemaps/android-maps-utils
And detect clicks to polyline using next method (in OnMapClickListener):
PolyUtil.isLocationOnPath(point, polyline.getPoints(), isGeodesic, tolerance);
With the recent update of the maps api, v8.4, introduces clickable Polyline
As mentioned in the doc:
Use the OnPolylineClickListener to listen to click events on a
clickable polyline. To set this listener on the map, call
googleMap.setOnPolylineClickListener(...). When
a user clicks on a polyline, you will receive an
onPolylineClick(Polyline) callback.
gradle-dependency:
compile 'com.google.android.gms:play-services-maps:8.4.0'
implement callback: GoogleMap.OnPolylineClickListener
initialize Polyline:
Polyline polyline = googleMap.addPolyline(options);
polyline.setClickable(true);
...
receive events
#Override
public void onPolylineClick(Polyline polyline) {
....
}
Happy coding :)
Register an OnMapClickListener. Determine if a given click is on your line yourself. If it is, do whatever it was you wanted to do in this case.
I had a similar issue where I could not process click events on polylines. I was using Xamarin for Android which is C# but the functionality is largely the same as the Android Java Libraries in this case.
In the end, I ended up doing what seemed to be the only option.
This involved processing all of the midpoints of my polylines(of which there were around 1300). On every OnMapClick, I took the LatLng of the click event and performed a distance formula between it and the midpoint of all polylines in the static List<PolylineOptions>. I then attached a map marker to the closest polyline.
From a tap on a polyline, it pops up a marker in about a quarter of a second.
I imagine the implemented marker click events from the Google Maps API work in a similar way.
Here is the for loop that handles finding the closest point to a click.
int i = 0;//create an indexer for the loop
double shortestDist = 100;//set an initial very large dist just to be safe
int myIndex = 0;//set variable that will store the running index of the closest point
foreach (PolylineOptions po in myPolylines) {
var thisDist = Distance (point, midPoint (po.Points [0].Latitude, po.Points [0].Longitude, po.Points [1].Latitude, po.Points [1].Longitude));//calculate distance between point and midpoint of polyline
if (thisDist < shortestDist) {
shortestDist = thisDist;//remember current shortest distance
myIndex = i;//set closest polyline index to current loop iteration
}
i++;
}
I know it isn't the prettiest code but it gets the job done. I didn't see a real answer to this anywhere on the internet so here it is. It could probably be made more efficient by calculating the midpoints beforehand and storing them in an equally sized list and then not having to call the midpoint formula for each polyline on every map click but it works really fast already.
EDIT
I do my testing on a galaxy s3 by the way, so I think it's not too inefficient.
If you are using com.google.android.gms:play-services-maps:8.4.0 then it includes polylines click listener
googleMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener()
{
#Override
public void onPolylineClick(Polyline polyline)
{
//do your work selected polyline
}
});
PolylineOptions line = new PolylineOptions();
Polyline polyline = googleMap.addPolyline(line);
polyline.setClickable(true);
only one question that i did not understand complety is that, on the page of google distance matrix, in the example of :
requesting distance and duration from Vancouver, BC, Canada and from Seattle, WA, USA, to San Francisco, CA, USA and to Victoria, BC, Canada.
in that part, what does it mean BC, WA etc. And in the request
http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false
Why Vancouver+BC and why not Seatle + WA.
And the most important question is that if i want to work with latidute and longitude, not with name of places, how can i do this?
Have a look at the answer i posted on this thread:
android google map finding distance
It uses the Google directions API but you still get the data you require, and uses Lat/Lon for the request. You would also need to add:
JSONObject duration = steps.getJSONObject("duration");
String sDuration = duration.getString("text");
int iDuration = duration.getInt("value");
in order to get the duration, make sure you put it after JSONObject steps = legs.getJSONObject(0);