I'm trying to create GeoPoints, but I couldn't so far.
double latitudeUpperLeft = -34.567645;
double longitudeUpperLeft = -58.497734;
double latitudeBottomRight = -34.62558;
double longitudeBottomRight = -58.42495;
GeoPoint geoPointUpperLeft = calculateGeoPoint(latitudeUpperLeft, longitudeUpperLeft);
GeoPoint geoPointBottomRight = calculateGeoPoint(latitudeBottomRight, longitudeBottomRight);
This is the helper method:
public static GeoPoint calculateGeoPoint(double latitude, double longitude) {
Double latE6 = latitude * 1E6;
Double lngE6 = longitude * 1E6;
return new GeoPoint(latE6.intValue(), lngE6.intValue());
}
I'm getting an InvocationTargetException on the return of the helper method, What could be wrong?
Thanks. Guillermo.
can you tell me if this works.
public static GeoPoint calculateGeoPoint(double latitude, double longitude) {
return new GeoPoint((int)(latitude * 1E6), (int)(longitude * 1E6));
}
I've found the answer in the anddev.org forum.
The problem was in the manifest, somehow it was missing this entry:
<uses-library android:name="com.google.android.maps" />
After adding it, it started to work.
If you want to use Directly GeoPoint .You will follow the steps:
1)First you have data(Location) in degree formate.
2)By the Equation you have to multiply it by 1000000
3)now ,you have particular GEO POINT Location .
Like here ;if I have location like : Latitude:23.0395677 and Longitude:72.5660045° So, your GeoPoint(23039568,72566045);
You get the Perfect Location for it.
Related
I'm currently trying to build an app where the user gets a notification when he is near to a marker or in my case I'm using the ItemizedOverlay of Osmdroid and I was wondering if there is a way to do that also for several hundreds of markers without emptying the battery in a few minutes.
I saw a few methods but there all were just for the case, when you have just a few markers.
I would be very happy if someone could help me.
To complement #Barns answer, 2 remarks:
when using osmdroid, you don't have LatLng, you have GeoPoint instead,
and you don't need to write your own getDistanceMeters because GeoPoint already has
this method:
GeoPoint.distanceToAsDouble(final IGeoPoint other)
If the Markers have static locations you won't need to load them repeatedly.
If all you want is to calculate the distance between two points on the earth and you have the coordinates in latitude and longitude then there is no need for any Google API's Maps or other libraries. That just costs extra overhead and maintenance. Just make a static method like this:
public static double getDistanceMeters(LatLng pt1, LatLng pt2){
double distance = 0d;
try{
double theta = pt1.longitude - pt2.longitude;
double dist = Math.sin(Math.toRadians(pt1.latitude)) * Math.sin(Math.toRadians(pt2.latitude))
+ Math.cos(Math.toRadians(pt1.latitude)) * Math.cos(Math.toRadians(pt2.latitude)) * Math.cos(Math.toRadians(theta));
dist = Math.acos(dist);
dist = Math.toDegrees(dist);
distance = dist * 60 * 1853.1596;
}
catch (Exception ex){
System.out.println(ex.getMessage());
}
return distance;
}
Then you can do this:
public static boolean checkDistanceIsClose(LatLng pt1, LatLng pt2, double distance){
boolean isInDistance = false;
try{
double calcDistance = getDistanceMeters(pt1, pt2)
if(distance <= calcDistance){
isInDistance = true;
}
}
catch (Exception ex){
System.out.println(ex.getMessage());
}
return isInDistance;
}
Same algorithm will work for any platform. Just translate it to the appropriate program language.
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());
I am facing a problem with the google map search. I have mad a app through which i can see a location in google map on my emulator which is set in the code as:
int lat = (int)(22.3666667*1000000);
int lng = (int)(91.8000000*1000000);
GeoPoint pt = new GeoPoint(lat,lng);
This works fine.
Now i want to search a location dynamically i.e. i have a editbox(Location_For_Search) and a button(Find_Location_Button). So when i write some location in editbox and press the button then it will show the location in google map with a marker on location. How can i do this?
Please any one help me.
With best wishes
Md. Fazla Rabbi
Use the geocoder:
Geocoder geo = new Geocoder(this);
List<Address> addr;
try
{
addr = coder.getFromLocationName(yourEditTextaddr, 10);
Address loc = addr.get(0);
loc.getLatitude();
loc.getLongitude();
point = new GeoPoint((int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
return point;
}
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));