jobin
jobin

Reputation: 1517

android imageview's position on map

I used an ImageView to place a marker on top of map so that it would be immovable when the map moves.How do I place ImageView in the center of the map(equator) when it opens. How do I get the latitude and longitude of the ImageView's position on the map. I searched on google but did not get the code.Please help me....

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jobinsabu.markeranimator.MainActivity">
    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map1"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"> </fragment>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:text="Map Animator"
        android:id="@+id/map_label"
        android:background="#70ffffff"
        android:textColor="#000000"
        android:textSize="25sp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="10dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
      <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:visibility="visible"
        android:src="@drawable/marker"
        android:id="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />
</RelativeLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    ImageView marker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        marker=(ImageView) findViewById(R.id.marker);
        SupportMapFragment supportMapFragment=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);               
        supportMapFragment.getMapAsync(this);}
     @Override
     public void onMapReady(final GoogleMap googleMap) {
        googlemap=googleMap;
        googlemap.getUiSettings().setCompassEnabled(true);
        googlemap.getUiSettings().setAllGesturesEnabled(true);
        googlemap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.getUiSettings().setZoomGesturesEnabled(true);
        }}

Upvotes: 2

Views: 1126

Answers (2)

W4R10CK
W4R10CK

Reputation: 5550

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    ImageView marker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        marker=(ImageView) findViewById(R.id.marker);
        SupportMapFragment supportMapFragment=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);               
        supportMapFragment.getMapAsync(this);}
     @Override
     public void onMapReady(final GoogleMap googleMap) {
        googlemap=googleMap;
        googlemap.getUiSettings().setCompassEnabled(true);
        googlemap.getUiSettings().setAllGesturesEnabled(true);
        googlemap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.getUiSettings().setZoomGesturesEnabled(true);
        Marker marker = googleMap.addMarker(new MarkerOptions()
        .position(lat_lng_of_equator)
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ImageView_of_marker)));
        googleMap.addMarker(marker);
        }
     }

Upvotes: 2

Naveen Kumar M
Naveen Kumar M

Reputation: 7557

You can add your own marker to googleMap

@Override
     public void onMapReady(final GoogleMap googleMap) {
        googlemap=googleMap;
        googlemap.getUiSettings().setCompassEnabled(true);
        googlemap.getUiSettings().setAllGesturesEnabled(true);
        googlemap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.getUiSettings().setZoomGesturesEnabled(true);
        Marker myMarker = map.addMarker(new MarkerOptions()
            .position(yourLocation)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
        googleMap.addMarker(myMarker);
        }}

Upvotes: 1

Related Questions