Using the MyLocationOverlay I get the current GeoPoint of the user.
myLocationOverlay.RunOnFirstFix (() => {
mapView.Controller.AnimateTo (myLocationOverlay.MyLocation);
RunOnUiThread (() => {
DisplayIncidentsNearMe (myLocationOverlay.MyLocation);}
);
}
);
However, To ReverseGeoCode that location, I need the Lat/Lng. The GeoPoint from the MyLocationOverlay is in LatitudeE6 format so doesn't work.
How do I get the "normal" lat/lng from the location object returned from the MyLocationOverlay?
Geocoder geo = new Geocoder (this, Java.Util.Locale.Default);
var ad = geo.GetFromLocation (myLocation.LatitudeE6, myLocation.LongitudeE6, 1);
From the GeoPoint documentation, LatitudeE6 is:
the latitude of this GeoPoint in microdegrees (degrees * 1E6).
You can convert this number back to degrees by dividing by 1e6:
double degrees = myLocation.LatitudeE6 / 1e6;
Related
Is it possible to restrict autocomplete places to return result only within 2km region from my current place? and within city?
I'm using code from official website I tried using set origin to my current location and bounds but I'm not getting desired result, by trying this I'm getting result from within country not from city and not from 2km from my current location, how can I achieve this?
Thanks In Advance
There will not be a direct method to specify the current location and radius, But you can achieve the same after simple data processing.
public void openPlacePickerActivity() {
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG);
//Specify your radius
double radius = 2.0;
// Get Rectangular Bounds from your current location
double[] boundsFromLatLng = getBoundsFromLatLng(radius, -33.880490f, 151.184363f);
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields)
// Specify Rectangular Bounds to restrict the API result.
// Ref: https://developers.google.com/places/android-sdk/autocomplete#restrict_results_to_a_specific_region
.setLocationRestriction(RectangularBounds.newInstance(
new LatLng(boundsFromLatLng[0], boundsFromLatLng[1]),
new LatLng(boundsFromLatLng[2], boundsFromLatLng[3])
))
.build(mActivity);
startActivityForResult(intent, 101);
}
/**
* Please check below link for an understanding of the method
* Ref: https://stackoverflow.com/questions/238260/how-to-calculate-the-bounding-box-for-a-given-lat-lng-location#41298946
*/
public double[] getBoundsFromLatLng(double radius, double lat, double lng) {
double lat_change = radius / 111.2f;
double lon_change = Math.abs(Math.cos(lat * (Math.PI / 180)));
return new double[]{
lat - lat_change,
lng - lon_change,
lat + lat_change,
lng + lon_change
};
}
i am getting data from gps provider using mylocation class. code is this:
MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {
#Override
public void gotLocation(Location location) {
//Got the location!
// for phone
//currentLocation = new GeoPoint((int) (location.getLatitude() * 1000000),
// (int) (location.getLongitude() * 1000000));
// for emulator
currentLocation = new GeoPoint((int) (location.getLatitude()),
(int) (location.getLongitude()));
doSomething();
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
when i use the app in emulator(2.3.3) it shows the correct location without multiplying anything.
but when i use it in a device(4.0) lat and lon need to multiplied with 1000000. i couldn't find why. i don't think its because of the version of android. anyone have any idea?
Because the MapView uses microdegress for its units so you need to multiply by 1e6. Otherwise you show up off the coast of Africa - basically lat long of approximately 0,0
From the documentation on GeoPoint:
An immutable class representing a pair of latitude and longitude, stored as integer numbers of microdegrees.
Don't know why the emulator is working - it shouldn't.
look at this:
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
GeoPoint myGeoPoint = myLocationOverlay.getMyLocation();
That works fine. But i need to save the coordinates in a variable. So i tried this:
myLocationLon = (double) myGeoPoint.getLongitudeE6();
When i run the App, this last line makes it collapse. Can you please tell me why this doesn't work ? Thank you
GeoPoint.getLongitudeE6() and GeoPoint.getLatitudeE6() both return microdegrees (basically degrees * 1E6).
so you need to convert microdegrees to degrees simply write function:
public double microDegreesToDegrees(int microDegrees) {
return microDegrees / 1E6;
}
and then
myLocationLon = microDegreesToDegrees(myGeoPoint.getLongitudeE6());
How can I get the Latitude and Longitude values of a particular location that I have long clicked on the map in Android?
For the long click, I suggest you check out http://www.kind-kristiansen.no/2010/handling-longpresslongclick-in-mapactivity/. This will go into detail on how to listen for long click events within the Maps API since there is little or no built-in functionality that I know of.
As for the lat/lng code, after you get the long click you can translate the pixels to coordinates.
public void recieveLongClick(MotionEvent ev)
{
Projection p = mapView.getProjection();
GeoPoint geoPoint = p.fromPixels((int) ev.getX(), (int) ev.getY());
// You can now pull lat/lng from geoPoint
}
You'll have to manage the LongClick event, and then use the code to find out longitude and latitude with the following code:
GeoPoint geoPoint=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
int latitude = geoPoint.getLatitudeE6();
int longitude = geoPoint.getLongitudeE6();
where 'event' is the object of 'MotionEvent'.
Use any other event according to your case.
It gives latitude and longitude on which point of map click
map.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
//myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
//The code below demonstrate how to convert between LatLng and Location
//Convert LatLng to Location
Location location = new Location("Test");
location.setLatitude(point.latitude);
location.setLongitude(point.longitude);
location.setTime(new Date().getTime()); //Set time as current Date
txtinfo.setText(location.toString());
//Convert Location to LatLng
LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions()
.position(newLatLng)
.title(newLatLng.toString());
map.addMarker(markerOptions);
}
});
I want to point to a Google map location using overlay. For this purpose latitude and longitude values will be assigned to a GeoPoint, but it only accepts int values.
How can I assign it a double value? Or is there another solution to point to an exact location?
point = new GeoPoint((int)t.getLati(),(int)t.getLongi())
Any help would be appreciated.
Since GeoPoint accepts latitudes and longitudes in microdegrees, simply create your point like so:
GeoPoint point = new GeoPoint((int)(latitude * 1e6),
(int)(longitude * 1e6));