I want to make google maps api to provide result of search by category as the nearest hotels available in certain range and i want also to control range , here is the code i started with , this code search when the use apply an address . i want this this to done when the user enter a certain category not by entering an address
protected void search(List<Address> addresses) {
Address address = (Address) addresses.get(0);
home_long = address.getLongitude();
home_lat = address.getLatitude();
latLng = new LatLng(address.getLatitude(), address.getLongitude());
addressText = String.format(
"%s, %s",
address.getMaxAddressLineIndex() > 0 ? address
.getAddressLine(0) : "", address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);
map1.clear();
map1.addMarker(markerOptions);
map1.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map1.animateCamera(CameraUpdateFactory.zoomTo(15));
locationTv.setText("Latitude:" + address.getLatitude() + ", Longitude:"
+ address.getLongitude());
}
what the modification needed to be done in this code ?
You have two options here:
1st option- You build your own DB with all the hotels and their locations, then reflect that on the map.
2nd option- you use Google's Places API. Here is a nice discussion about it.
Hope this helps :)
Related
I want to get values from address of Place Picker in android and separate it into different EditText like I want to set pincode on PineCodeEditText, set country on CountryEditText, set State on StateEditText and so on. I have got address as
10, Mothorowala Rd, Jagdamba Colony, Dharmpur, Ajabpur Kalan,
Dehradun, Uttarakhand 248001, India
Where I used
Place place = PlacePicker.getPlace(data,this);
String address = String.format(String.valueOf(place.getAddress()));
Before this I used Split function but it gives indexOutOfBoundException.
Any suggestion please welcome, Thank You.
You can get the latitude and longitude coordinates from the Place object, then get the address object using the GeoCoder, which you can use to get the required data separately.
LatLng coordinates = place.getLatLng(); // Get the coordinates from your place
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(
coordinates.latitude,
coordinates.longitude,
1); // Only retrieve 1 address
Address address = addresses.get(0);
String countryCode = address.getCountryCode();
String countryName = address.getCountryName();
In my Android application i need to get the traveled road with car and i check it with google android directions API and GPS.
But the problem is that some times the GPS location is not precise and i can get an error of 10Km for some lecture and that means that in a total travel of 150km my "distance traveled" can be 250km and it's wrong!
Yesterday i test it in highway and the problem is that when the marker is located out from highway, the calculation of distance traveled from my current position and the last marker located by road is very wrong (in this case out of the highway).
There is some best way for getting traveled distance with car using the phone?
Maybe some better code for getting more precise GPS position?
Here is my code:
private void getUserLocation() {
Log.d(TAG, "getUserLocation()");
checkLocationPermission();
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
String stringStartLat, stringStartLng;
double lat = location.getLatitude();
double lng = location.getLongitude();
//Start position of device
if (travelInfo.getStartLat() == 0 && travelInfo.getStartLng() == 0) {
travelInfo.setStartLat(lat);
travelInfo.setStartLng(lng);
//Set lastcoordinates equals to start
travelInfo.setLastLat(lat);
travelInfo.setLastLng(lng);
}
//Current position of device
travelInfo.setCurrentLat(lat);
travelInfo.setCurrentLng(lng);
stringStartLat = Double.toString(travelInfo.getStartLat());
stringStartLng = Double.toString(travelInfo.getStartLng());
//Set the TextView in the fragment with start coordinates
Log.d(TAG,"LatitudeStart: " + stringStartLat);
Log.d(TAG,"LongitudeStart: " + stringStartLng);
if (startMarker == null) {
//Add the user location to the map with a marker
LatLng userLoction = new LatLng(travelInfo.getStartLat(), travelInfo.getStartLng());
startMarker = googleMap.addMarker(new MarkerOptions()
.position(userLoction)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
.title("Start Position")
.snippet("Show the start position of user"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(userLoction).zoom(9).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
} else {
LatLng currentLocation = new LatLng(travelInfo.getCurrentLat(), travelInfo.getCurrentLng());
currentPositionMarker = googleMap.addMarker(new MarkerOptions()
.position(currentLocation)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
.title("Current Position")
.snippet("Show the current position of user during travel"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(currentLocation).zoom(11).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
if (travelInfo.getEndLat() != 0 && travelInfo.getEndLng() != 0) {
Log.i(TAG, "Dentro if userlocation");
//Get total distance of travel
getTotalDistanceTime(travelInfo);
//Get the percurred distance from last known coordinates
getPercurredDistance(travelInfo);
}
}
}
});
}
And getpercurredDistance use google direction API with this url
String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + obj.getLastLat() + "," + obj.getLastLng() + "&destination=" + obj.getCurrentLat() + "," + obj.getCurrentLng() + "&mode=driving&key=AI*******************I";
For getting distance traveled from last marker and current position marker.
But some times it put the localized position too far from real position... how avoid this?
EDIT
The method that i use is good for my work? mFusedLocationClient.getLastLocation()
There is no possibilities to get more precise GPS position from GPS sensor of android device, but you can filter "wrong" points this ways:
1) for each travel point get several values of Lat and Lng, discard the minimum and maximum and average the remaining ones;
2) log timestamps of each travel point and compare it for two neighboring points: speed (distance between that two points divided by time interval of that points) grater than max. car speed (e.g. 250 km/h) that means the second point is wrong (if first point is good of course).
3) use Google Maps Roads API to find best-fit road geometry for a given set of GPS coordinates.
Also take a look at this question.
For real-time location purposes, location updates allow you to request location updates at different intervals. See the android developer location documentation. The function you want is FusedLocationProviderClient.requestLocationUpdates.
There is no reason GPS (or any other widely used location technology) should ever give you 10km error for a single location estimate. If you are getting this, there is a large mismatch between when you the location estimate was made and when you are requesting it.
I have the issue with some location that donot return me the address while other returns me the address i have issue with the following address
List addresses = geocoder.getFromLocationName(
"568 S. Coy St., Kansas City, Kansas, 66105", 1);
List addresses = geocoder.getFromLocationName(
"10075 Bauer Rd., St. Louis, Missouri, 63128", 1);
I just try to get the Address with the hardcode string but i got failure to get the address , While if i remove the initial address it return me the address of location while if i put this on the web page of google map it shows me the marker on the actual location where the place is.
I want the street name and longitude latitude of entered pin code in edit text as shown in the image of google map. I want to develop this type of feature in android.
Thank you in advance.
Try this,
Geocoder geo = new Geocoder(getApplicationContext(),
Locale.getDefault());
List<Address> add;
try {
add = geo.getFromLocation(taplat, taplon, 1);
addstr = add.get(0).getPostalCode();
}
I implemented the reverse geocoding in my app and it is working, but sometimes it happens a very strange issue.
The code is that
List<Address> addresses = geo.getFromLocation(
obj.getLatitude(), obj.getLongitude(), 1);
List<Address> address = geo.getFromLocationName( addresses.get(0).getLocality().getBytes() , 1 );
Address location = address.get(0);
In the first part I get the address object of the place in which I'm. Than I want recover the generic coordinates of the city in where I'm because I don't want store the coordinates of my exact position.
This is working but I encounter a very strange issue! Trying the app with the fakegps app I set my position in "Ñuñoa", and the first address was found correctly, but when I try to get the generic coordinates, I get "Nunoa" that isn't in Chile, but in Peru!!
That makes no sense! why this?
Thanks for helping me
Sure Swathi.
List<Address> addresses = geo.getFromLocation(msg_r.getLatitude(),
msg_r.getLongitude(), 1);
String geoL = addresses.get(0).getLocality() + ", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryCode();
// reverse-reverseGeocoding
List<Address> address = geo.getFromLocationName(geoL, 1);
System.out.println("Where I am? " + geoL);
Address location = address.get(0);
// generic coordinate for the locality/city/town
location.getLatitude();
location.getLongitude();