Get geographic location from screen location - android

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.

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

Move map to user's position

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));

how to open up a map of a particular city using google APIs in android

I have been able to open up a map of the world through code and mark a few places. However, I always have to zoom down to the city, which takes a little while. Is there any way to directly open up a map of a particular city?
You can use moveCamera with your last stored location (lat, lng and maybe zoomLevel too) for example.
final LatLng location = new LatLng(..., ...);// Loaded from SharedPreferences maybe.
// Move the camera to location with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));
You can use this sample code to animate the map directly to your city coordinate (lat, lng) and automatically zooming out to specified level :
// Set desired lat lng location, and zoom level (for example 10)
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(locationCity.getLatitude(), locationCity.getLongitude()), 10);
// Move the camera to location
gMap.animateCamera(cameraUpdate);

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/

code to add markers to map using android google maps v2

I have lat&long values from database.how to display markers based on lat &long values using android google map api v2.In original android google maps,we display markers based on concept itemoverlay. In v2,I dont know how to display markers.
dbAdapter.open();
Cursor points = dbAdapter.clustercall(Btmlft_latitude, toprgt_latitude,
Btmlft_longitude, toprgt_longitude, gridsize1);
int c = points.getCount();
Log.d("count of points", "" + c);
if (points != null) {
if (points.moveToFirst()) {
do {
int latitude = (int) (points.getFloat(points
.getColumnIndex("LATITUDE")) * 1E6);
int longitude = (int) (points.getFloat(points
.getColumnIndex("LONGITUDE")) * 1E6);
mapView.addMarker(new MarkerOptions().position(
new LatLng(latitude, longitude)).icon(
BitmapDescriptorFactory.defaultMarker()));
} while (points.moveToNext());
}
}
points.close();
dbAdapter.close();
That is mycode.I am getting lat&long values from database,but how to add markers based on lat &long values to map.
I read the android google maps api v2.In that have only give statically added data of markers
Use the Marker Class
An icon placed at a particular point on the map's surface. A marker icon is drawn oriented against the device's screen rather than the map's surface; i.e., it will not necessarily change orientation due to map rotations, tilting, or zooming.
A marker has the following properties:
Anchor
The point on the image that will be placed at the LatLng position of the marker. This defaults to 50% from the left of the image and at the bottom of the image.
Position
The LatLng value for the marker's position on the map. You can change this value at any time if you want to move the marker.
Title
A text string that's displayed in an info window when the user taps the marker. You can change this value at any time.
Snippet
Additional text that's displayed below the title. You can change this value at any time.
Icon
A bitmap that's displayed for the marker. If the icon is left unset, a default icon is displayed. You can specify an alternative coloring of the default icon using defaultMarker(float). You can't change the icon once you've created the marker.
Drag Status
If you want to allow the user to drag the marker, set this property to true. You can change this value at any time. The default is true.
Visibility
By default, the marker is visible. To make the marker invisible, set this property to false. You can change this value at any time.
GoogleMap map = ... // get a map.
// Add a marker at San Francisco.
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
You have to use the method GoogleMap.addMarker(MarkerOptions);
I've explained this in a tutorial on my blog:
http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html
On this blog, you'll find some more posts concerning the new Maps API:
Move the map to current or some location
Create the custom InfoWindow
Associate geodata or objects to a marker
I'm guessing the issue is that you are multiplying your latitude/longitude values by 1E6 instead of dividing them by 1E6. If your stored values came from GeoPoints, that might be your issue. Otherwise, the way you are adding the Markers is fine and should work.
i.e. the lat/lng values you pass to new LatLng() should look like 45.4,-95.5 NOT 45400000,-95500000
Check out the following code:
//mMap is a GoogleMap and point is a GeoPoint
this.mMap.addMarker(getMarker(point, drawableId))
...
public MarkerOptions getMarker(GeoPoint point, int drawableId)
{
return new MarkerOptions()
.position(new LatLng(point.getLatitudeE6() / 1000000.0, point.getLongitudeE6() / 1000000.0))
.icon(BitmapDescriptorFactory.fromResource(drawableId));
}

Categories

Resources