How to get the Latitude and Longitude on Map in Android? - android

How can I get the Latitude and Longitude values of a particular location that I have long clicked on the map in Android?

For the long click, I suggest you check out http://www.kind-kristiansen.no/2010/handling-longpresslongclick-in-mapactivity/. This will go into detail on how to listen for long click events within the Maps API since there is little or no built-in functionality that I know of.
As for the lat/lng code, after you get the long click you can translate the pixels to coordinates.
public void recieveLongClick(MotionEvent ev)
{
Projection p = mapView.getProjection();
GeoPoint geoPoint = p.fromPixels((int) ev.getX(), (int) ev.getY());
// You can now pull lat/lng from geoPoint
}

You'll have to manage the LongClick event, and then use the code to find out longitude and latitude with the following code:
GeoPoint geoPoint=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
int latitude = geoPoint.getLatitudeE6();
int longitude = geoPoint.getLongitudeE6();
where 'event' is the object of 'MotionEvent'.
Use any other event according to your case.

It gives latitude and longitude on which point of map click
map.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
//myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
//The code below demonstrate how to convert between LatLng and Location
//Convert LatLng to Location
Location location = new Location("Test");
location.setLatitude(point.latitude);
location.setLongitude(point.longitude);
location.setTime(new Date().getTime()); //Set time as current Date
txtinfo.setText(location.toString());
//Convert Location to LatLng
LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions()
.position(newLatLng)
.title(newLatLng.toString());
map.addMarker(markerOptions);
}
});

Related

How to Zoom and get latitude and longitude from mMap.setMyLocationEnabled(true) in Google Map (Android)

I was setting mMap.setMyLocationEnabled(true) and I am successfully getting my current location but...
Not able to zoom to that position.
Not able to get lat and lng from that position
From getting latitude and longitude I am using below code but it gives me null. I am calling below method from onMapReady(Google Map) and passing google map object.
private void goToCurrentLocation(GoogleMap map) {
Log.d("Tag", "inside");
Location loc = map.getMyLocation();
if (loc != null) {
LatLng point = new LatLng(loc.getLatitude(), loc.getLongitude());
Log.d("Tag=", "latlng=" + point);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point, 15));
}
}
Is there any alternative to getting latitude and longitude as getMyLocation deprecated?
use link provided in the answer to get current location and then zoom over that location using below code:
latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
mGoogleMap.animateCamera(cameraUpdate);
http://blog.teamtreehouse.com/beginners-guide-location-android

Find the markers position in google map while moving camera positions

How to find the markers and markers positions (Lat,Lan) on Google map while moving the camera position.?
Using oncamera change listener you can get latitude and longitude
map.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
LatLng latvalue = position.target;
double lat = latvalue.latitude;
double lng = latvalue.longitude;
}
});

Android Google Map showing multiple markers on current location

Hello everyone I am using custom marker image in google map. My problem is that when i click on that marker or zoom in or zoom out this marker duplicates means, more markers appears on the same location on different points. why this happening? how to avoid it?
Thanks in advance.
Code
#Override
public void onLocationChanged(Location location)
{
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
latitude = location.getLatitude();
// Getting longitude of the current location
longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
MarkerOptions markerOptions = new MarkerOptions();
// Setting position for the marker
markerOptions.position(latLng);
// Setting custom icon for the marker
// markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.compass_needle));
// Setting title for the infowindow
// Adding the marker to the map
googleMap.addMarker(markerOptions);
// markerOptions.rotation(260);
}
You are adding markers but you are not clearing previous added loation marker so You have to clear previously added marker when Location is changed again.
Add this line above googleMap.addMarker(markerOptions);
googleMap.clear();
googleMap.addMarker(markerOptions);
I hope it helps!
More correct is to remove a specific marker, and don't clear the whole map.
Because you can have others markers and polylines on map.
In your case you have only one marker showing user location.
You can get it when adding.
private Marker mLocationMarker;
public void onLocationChanged(Location location) {
...
if(mLocationMarker != null) {
mLocationMarker.remove();
}
mLocationMarker = googleMap.addMarker(markerOptions);
}

Showing multiple markers in google map v2

i have a set of lat long coordinates. i want to display all of those markers at a time on google map. I know how to display a single marker. But don't know how to display multiples at once. Anybody please help me.
Thank you.
i have lat long points.
like
id = 123 lat = 12.00 lon = 77.00
id = 124 lat = 12.01 lon = 77.01
etc.
May This Help You
for(int pin=0; pin<pins.size(); pin++)
{
LatLng pinLocation = new LatLng(Float.parseFloat(pins.get(pin).latitude), Float.parseFloat(pins.get(pin).longitude));
Marker storeMarker = map.addMarker(new MarkerOptions()
.position(pinLocation)
.title(pins.get(pin).pinname)
.snippet(pins.get(pin).address)
);
}
Try this:
LatLng one = new LatLng(2.40744, 77.014702);//Latitude and long points
LatLng two = new LatLng(2.407440, 77.014702);
.......
Similarly u can use more lat and long
myMarkerOne = gm.addMarker(new MarkerOptions()
.position(one)//use LatLng obj
.title("C")
.snippet("dsfd")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
myMarkerTwo = gm.addMarker(new MarkerOptions()
.position(two)
.title("C")
.snippet("dsfds")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
refer this: How to add multiple marker and overlay in v2 google map?
Also check this link:
Create a LatLng object by using following format:
LatLng location = new LatLng(latitude, longitude);
Then call the method with your LatLng object along with your GoogleMap object:
private Marker setCurrentLocation(LatLng location, GoogleMap map) {
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
.fromResource(R.drawable.user_location_pin);
Marker marker = map.addMarker(new MarkerOptions().position(location)
.icon(bitmapDescriptor).title(location.toString()));
return marker;
}

How to get latitude and longitude of map in using Android google mapV2

I am looking for latitude and longitude values of top right corner of mapView using Android google maps V2. I posted similar question on the same issue, but no use.
I wrote code for this,but getting zero values.
please check my code:
MapView mapView = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLngBounds bounds = mapView.getProjection().getVisibleRegion().latLngBounds;
LatLng topright = bounds.northeast;
double toprgt_latitude = topright.latitude;
Log.d("top lat", "" + toprgt_latitude);//getting zero values.
Use mapView.getProjection().getVisibleRegion().latLngBounds.northeast after map is shown.
LatLng corner;
mapView.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
corner = mapView.getProjection().getVisibleRegion().latLngBounds.northeast;
}
}
LatLng currentposition = new LatLng(location.getLatitude(), location.getLongitude());
sys.out.println(""+location.getLatitude()""+location.getLongitude());

Categories

Resources