Home > database >  How to open Google Map by clicking on a image Android Studio
How to open Google Map by clicking on a image Android Studio

Time:03-16

I have a image (image of a map) in a activity and I want to open Google Map on the phone with certain geo coordinates. I have this following code that run fine and my application is lunching, but when I click on the image my app is restarting. I checked other questions and my code seems fine so I don't have any idea of what could be wrong.

MilleActivity.java

public class MilleActivity extends AppCompatActivity {


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mille);

        ImageView map = (ImageView) findViewById(R.id.mapQC);
        map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMap();
            }
        });



      }

    private void openMap() {
        String uri = String.format("geo:46.81402244651992, -71.220484138461");
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        intent.setPackage("com.google.android.maps");
        startActivity(intent);
    }


}

//}

activity_mille.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MilleActivity"
    android:background="@color/white"
    android:paddingTop="50dp">

    <ImageView
        android:layout_width="350dp"
        android:layout_height="350dp"
        android:gravity="center"
        android:scaleType="centerInside"
        android:src="@drawable/mille"
        android:layout_centerHorizontal="true"
        tools:layout_editor_absoluteY="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="350dp"
            android:text="Film: 1987"
            android:textColor="#000"
            android:textSize="25sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="Réalisateur: Ricardo Trogi"
            android:textColor="#000"
            android:textSize="25sp" />


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:text="Lieu: Québec"
            android:textColor="#000"
            android:textSize="18sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="Anecdote: Ricardo Trogi (réalisateur)"
            android:textColor="#000"
            android:textSize="18sp" />

        <ImageView
            android:id="@ id/mapQC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:baselineAlignBottom="true"
            android:scaleType="centerInside"
            android:src="@drawable/map" />

    </LinearLayout>

</RelativeLayout>

CodePudding user response:

try this code

String latitude = "46";
String longitude = "71";
String uri = "https://www.google.com.tw/maps/place/"   latitude   ","   longitude;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);

You can replace latitude and longitude you want

  • Related