How to display user location on osmdroid mapView - android

I'm creating a GPS like application with omsdroid, I successfully displayed a map and centered it on the user location, but I can't find a way to display an "user icon" on the map.
I'm new on android (started like one week ago) so please explain all step and add sample of code if you can, thanks.
edit :
here is my code so far :
edit :
here is my code so far :
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
altitude = location.getAltitude();
accuracy = location.getAccuracy();
GeoPoint p = new GeoPoint( (latitude ), (longitude ));
myMapController.animateTo(p);
myMapController.setCenter(p);
mylocation = new MyLocationNewOverlay(new GpsMyLocationProvider(getApplicationContext()), mapView);
mylocation.enableMyLocation();
//mylocation.enableFollowLocation();
mapView.getOverlays().add(mylocation);
}

For enable user location icon you should add the MyLocationOverlay on your map:
org.osmdroid.views.MapView openMapView = (org.osmdroid.views.MapView) v.findViewById(R.id.openmapview);
MyLocationOverlay myLocationoverlay = new MyLocationOverlay(getActivity(), openMapView);
myLocationoverlay.enableMyLocation();
mMapView.getOverlays().add(mMyLocationNewOverlay);

Related

storing Geopoints into Parse.com

I am trying to store my location into the parse.com database, but i have having an issue doing so. First of all, I can't store anything into the database I believe it is returning null or something so. Not quite sure whats the behavior. Can someone guide me in the right direction?
double longitude = location.getLongitude();
double latitude = location.getLatitude();
// Get longitude of the current location
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
ParseObject parkingobject = new ParseObject("Cooking");
parkingobject.put("username","Alana");
ParseGeoPoint point = new ParseGeoPoint(latLng); //<----- This is where the error is.
parkingobject.put("Location", point);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
ParseGeoPoint geoPoint = new ParseGeoPoint(latitude , longitude );
ParseObject parkingobject = new ParseObject("Cooking");
parkingobject.put("username","Alana");
parkingobject.put("Location", geoPoint );
Try this code.

How to get nearby place in android when getting mylocation by lat and long

I am working on application where i get MyLocation by Latitude and Longitude .Now I want to get the Users who is in 5 Km within my range .I am using following code to get the Lat and Long of User.
if(gps.canGetLocation()){
getlatitude = gps.getLatitude();
getlongitude = gps.getLongitude();
latitude_mString = String.valueOf(getlatitude);
longitude_mString = String.valueOf(getlongitude);
Log.d("Value of lat and long", latitude_mString+""+longitude_mString);
Log.d("u r", "ur in register lati loni");
new Thread(null,latlongThread,"").start();
}
}else{
gps.showSettingsAlert();
}
marker = new MarkerOptions().position(new LatLng(getlatitude, getlongitude)).title("Me");
//marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.));
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(getlatitude, getlongitude))
.zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
googleMap.addMarker(marker);
return rootView;
}
By This Code i can get mylocation .I am storing each user data in my API .So i have all data regarding lat and long of each user .So now i want to print user who have location or lat and long within 5 km of me.
Use the location's distanceTo method
http://developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location)

How to show received latitude/longitude from server on google map?

How to show latitude and longitude which I received from server on android map. I implemented google map V2 but don't know how to show gps coordinates on map. I came here after a lot of research and wasn't able to find a single reasonable solution for it. Can anyone please help me out?
So far my code of latitude and longitude values which I am reviving from server.
String lat, lon;
double l1,l2;
DB db = new DB(getApplicationContext());
HashMap<String,String> gps = new HashMap<String, String>();
gps = db.getGps();
lat = user.get("lati");
lon = user.get("longi");
Okay when I try to add marker on map according to latitude and longitude from server my app got force close and gives NullPointerException.
free.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String lat, lon;
double l1,l2;
DB db = new DB(getApplicationContext());
HashMap<String,String> gps = new HashMap<String, String>();
gps = db.getGps();
lat = user.get("lati");
lon = user.get("longi");
l1 = Double.parseDouble(lat);
l2 = Double.parseDouble(lon);
map.addMarker(new MarkerOptions().position(new LatLng(l1, l2))
.title("My Map Title"));
Your "map" variable may not be initialized. I add a marker like this:
SupportMapFragment smf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = smf.getMap();
if ( mMap != null ) {
LatLng newLatLong = new LatLng(dblLat, dblLong);
mMap.addMarker(new MarkerOptions().position(newLatLong)
.title(name)
.snippet(fullStrAddr));
}
Add a marker like this
Marker mymarker= map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
.title("My Map Title"));
This will set a marker at the latitude and longitude which was sent from the server..
Try it..

How can i put a pointer on a map of my entered address. Android

I want to show a pointer on a map after entering address. What would be best solution ? Couldn't find any tutorials on this problem. I found a code to get latitude and longitude, what to do next ?
Geocoder coder = new Geocoder(this);
List<Address> address;
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
}
addmarker() function will help you to achieve this
myMap.addMarker(new MarkerOptions().position(yourLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.yourmarkericon)));
where myMap is the Object of GoogleMap and yourLatlng is Latlng Where you want to add marker and yourmarkericon is icon which you want to display on map. Use blow link for more information
https://developer.android.com/reference/com/google/android/gms/maps/model/Marker.html

Map marker doesn't point to exact location

So I'm storing markers from my map into a sqlite database. I am trying to query the markers by latitude and longitude. So for testing I am adding the marker to a specific location. When I check the markers position it gives me another location. What is causing this?
private void drawMarker(LatLng point, String title, String snippet){
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
LatLng l = new LatLng(37.52100000000001, -77.44800000000001);
markerOptions.position(l);
markerOptions.title(title);
markerOptions.snippet(snippet);
// Adding marker on the Google Map
googleMap.addMarker(markerOptions);
}
This is the code to extract the marker position
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
marker1 = marker;
LatLng mlatlong = marker1.getPosition();
markerLat = Double.toString(mlatlong.latitude);
markerLng = Double.toString(mlatlong.longitude);
latitude = mlatlong.latitude;
longitude = mlatlong.longitude;
Log.i("Marker", mlatlong.latitude +","+mlatlong.longitude);
markerDialog();
}
});
This gives me the position
06-20 21:53:52.447: I/Marker(15221): 37.52100000569583,-77.44800008833408
In Java, and most other languages you'll come accross, double is not stored as a precise value.
You can read more here.
Also, it shouldn't really matter the locations you posted are extremely close to each other.

Categories

Resources