private void initMyLocation() {
myLocOverlay = new MyLocationOverlay(this, myMap);
myLocOverlay.enableCompass();
myLocOverlay.enableMyLocation();
myMap.getOverlays().add(myLocOverlay);
if (myLocOverlay == null)
{
assignFirstLoc();
}
if (myLocOverlay != null)
{
Log.e("error","1");
p = new GeoPoint((int) (myLocOverlay.getMyLocation().getLatitudeE6()), (int) (myLocOverlay.getMyLocation().getLongitudeE6()));
mc = myMap.getController();
mc.animateTo(p);
mc.setCenter(p);
}
I saw "error 1" message in the logcat but after that I took an null pointer exception I dont know why, I believe I check myLocOverlay is null or not with "if" block.. Any suggesstions?
Edit:
This is my assignedGFirstLoc method that assigned the firtst values:
private void assignFirstLoc()
{
final String coordinates[] = {"41.00527", "28.97696"};
double lat2 = Double.parseDouble(coordinates[0]);
double lng2 = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int) (lat2 * 1E6), (int) (lng2 * 1E6));
myMap.getController().setZoom(16);
myMap.getController().animateTo(p);
myMap.getController().setCenter(p);
}
MyLocationOverlay can only be used to show a location coming from sensors.
You cannot set it.
You got a NullPointer because MyLocationOverlay.getMyLocation() returns null. Sensors didn't have the time to catch the geolocation yet.
If you want to show the location when the sensor got the data you can do this:
mMyLocationOverlay.runOnFirstFix(new Runnable() { public void run() {
mapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
}});
If you want to display an item in an arbitray location (with latitude and longitude values) you need to implement your own version of ItemizedOverlay
You can find an exemple in the SDK documentation: Google Map View tutorial, Part2 AddingOverlayItem
If you just want to show an arbitrary location on the map, you don't need an overlay. just call your function and it will work:
private void initMyLocation() {
assignFirstLoc();
}
It looks like you are not passing any coordinates into GeoPoint. Unless it is somewhere else in your code. Something like this works
String coordinates[] = {"41.146064", "-80.642861"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
Related
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'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.
how to coding to set mapview don't show the current location
now it's not show on emulater but when I try to install on my mobile , the current location sigh will show
What should I do?
public static int getCoordinateE6(double coordinate) {
return (int) (coordinate * 1E6);
}
GeoPoint geoPoint = new GeoPoint(
GoogleMapServiceHelper.getCoordinateE6(latitude),
GoogleMapServiceHelper.getCoordinateE6(longitude));
getMapView().getController().animateTo(geoPoint);
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;
}