Move map to user's position - android

I thought this would be really simple but how can you move the map to the user's current location?
map.getMyLocation() seems to be returning a null value.

First of all the current location of the user you should get it from a LocationListener after you have the location use this to set it on the map
// Enabling MyLocation Layer of Google Map
map.setMyLocationEnabled(true);
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
map.animateCamera(CameraUpdateFactory.zoomTo(15));

Related

Move the marker along the with map as the user moves

I have implemented google map into my app. I have added a custom marker at my current location.I want to move the marker as the user moves, along with the map. I want to implement the functionality as in google maps navigation. While googling i found the following links but did not get the result. so can any one help me out ?
I have added this in onLocationChanged()
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
sourceLocation = latLng;
startPoint = new GeoPoint(latLng.latitude,latLng.longitude);
forNavigation = location;
// animation
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mPositionMarker.getPosition(), 15));
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location
.getLatitude(), location.getLongitude())));
These are the links which i found
Google map: moving marker and map together smoothly along with user?
How to smoothly keep moving current location marker in Google Maps v2 android
here is two solution you have.
Use onlocationtrue then it show you blue dot as your current location.
Use fused api for current location and create you marker in locationchanged.
for fused api you can check from here.
thanks

Get geographic location from screen location

I'm working on an application that uses google maps. Can I get the geographic location of a particular point on the screen?.For example, I have a view (button, image, etc) with a X and Y position on the screen. What I want to know is the geographic location (latitude and longitude) of that position (view position).
Of course this position (X and Y) is on the map.
I found the solution. This is how:
Projection projection = googleMap.getProjection();
// Returns the geographic location that corresponds to a screen location
LatLng geographicalPosition = projection.fromScreenLocation(new Point(your_X, your_Y));
// Returns a screen location that corresponds to a geographical coordinate
Point screenPosition = projection.toScreenLocation(new LatLng(your_latitude, your_longitude));
More information here.
In short yes(your questions lacks details and your own attempt), I would drop a marker on the screen at my current location and set the marker property to draggable. This will allow me to move the marker to any position on the map
For example,
private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(Your_Lat, Your_Lng))
.title("My Marker")
.draggable(true));
After placing it anywhere you want you can retrieve the markers new Latitude and Longitude by simply using the position function. Something like this.

android automatic title on red marker on Google Maps

I have a android map application where it track the user location and display it on the map. i make it so that there will be a title "you are here" that appear on top of the red marker. however, it did not appear on its own and i must tap on the red marker. and i could tap on any red marker and the title would move there and disappear on the previous one.
how do i make it so that it only appears on the latest red marker(latest location) on its own and cannot be removed?to show the user his current location.
the speed i set it at 1. i do not know whether it is good enough. the location stopped updating while i was not moving but it is still slow and inaccurate.
public void onLocationChanged(Location myLocation) {
System.out.println("speed " + myLocation.getSpeed());
if(myLocation.getSpeed() > speed)
{
//show location on map.................
//Get latitude of the current location
double latitude = myLocation.getLatitude();
//Get longitude of the current location
double longitude = myLocation.getLongitude();
//Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
//Show the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).snippet("You are here!").title("You are here!"));
Try This:-
Marker pos = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).snippet("You are here!").title("You are here!"));
instead of
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).snippet("You are here!").title("You are here!"));
Or
Write this, it is very easy:-
locationMarker.showInfoWindow();
You can't really achieve this by using the existing API's on Android, without coding a lot of custom views and controls.
There's a nice library which you can use to customize the markers on your maps. Here's an example taken from it's website. It's called android-maps-utils
The link to the library on github.
https://github.com/googlemaps/android-maps-utils
And the website, where you can find a video and other examples.
http://googlemaps.github.io/android-maps-utils/

current position in on map android

I am building an app in which there is an activity to search games and players and when this activity starts a map also starts.My problem is that the map does not shows current position unless and untill we click on the show my location button..What I want to do is that on start of activity my map will point to my current position as in Google Maps on our android devices along with a pointer or marker ?
Tried this?
LatLng latLng = new LatLng(curlat, curlong); // LatLng current location
gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.animateCamera(CameraUpdateFactory.zoomTo(20));
gMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("My location"));

How to update google map v2 location from background service android

I have a background service that implements LocationListener and will provide Location updates periodically. I want to then use those location updates to update the user position on a map in the main activity. I'm able to pass the latitude longitude data via an intent to the main activity, but I don't know how to use that data to display the user's location.
Check out this link:
https://developers.google.com/maps/documentation/android/
Specifically these lines
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

Categories

Resources