Reputation: 2110
I'm trying to get location from GoogleMap using ImageView over it. I can drag this Pin image and the position where I drop it,I should get its location.
I'm using framelayout with googleMap as it's first child and ImageView(i.e Pin) as its second child element.
The same like we have in Google maps application.
.
Please suggest me, how to achieve this?
My layout file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/fetchaddressMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.MapFragment" />
<ImageButton
android:id="@+id/markerRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:contentDescription="@string/content_description"
android:src="@drawable/marker_red"
android:visibility="visible" />
</FrameLayout>
Upvotes: 4
Views: 6721
Reputation: 3021
You need to implement OnMarkerDragListener class. You can also get the street adress and other informations. Read the docs.
http://developer.android.com/reference/android/location/Geocoder.html http://developer.android.com/reference/android/location/Address.html
mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
marker.getPosition();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(marker.getPosition().latitude, marker.getPosition().longitude, 1);
String city = addresses.get(0).getAddressLine(1);
Toast.makeText(MapsActivity.this, city, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Use android's pins for better and easy implementation. However, if you want to use customs pins then you dont need to add that imageview under the framelayout. Delete that imageview. See this link to see how to customize a marker.
I hope it helps.
Upvotes: 0
Reputation: 674
I would recommend you tu use the Markers that are already built in with the Google Maps Api, you just need to set the marker as draggable, and add a listener, the callbacks will provide you with all the information you need. Further information is in the documentation. Also you can use the Android Maps Utils library to define custom markers.
Upvotes: 2
Reputation: 44118
I'm guessing you fetch the screen's rawX
and rawY
points via a MotionEvent
object received in an OnTouchListener
.
With those 2 coordinates you can find the location on the map with the help of a Projection
:
Projection projection = googleMap.getProjection();
Log.e("TAG", projection.fromScreenLocation(new Point(x, y)).toString());
Upvotes: 1