I tried to create a simple map app in Android Studio

Based on the technical book, I created a simple map display application.

MainActivity.java


package com.example.implicintintentsample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    //Get latitude and longitude information from your current location
    private double _latitude = 0;
    private double _longitude = 0;

    private TextView _tvLatitude;
    private TextView _tvLongitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//Get the contents of the TextView field that displays latitude and longitude.
        _tvLatitude = findViewById(R.id.tvLatitude);
        _tvLongitude = findViewById(R.id.tvLongitude);

//Get LocationManager object
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

//Generate a listener object when the location information is updated
        GPSLocationListener locationListener = new GPSLocationListener();

//Start tracking location information
        if (ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
            ActivityCompat.requestPermissions(MainActivity.this,permissions, 1000);
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }


//What happens when you press the map search button
    public void onMapSearchButtonClick(View view) {
        EditText etSearchWord = findViewById(R.id.etSearchWord);
        String searchWord = etSearchWord.getText().toString();

        try{
            searchWord = URLEncoder.encode(searchWord, "UTF-8");

            String uriStr = "geo:0,0?q=" + searchWord;
            Uri uri = Uri.parse(uriStr);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }

        catch(UnsupportedEncodingException ex){
            Log.e("IntentStartActivity", "Search keyword conversion failure", ex);
        }
    }

//What happens when you press the map display button
    public void onMapShowCurrentButtonClick(View view){
        String uriStr = "geo:" + _latitude + "," + _longitude;
        Uri uri = Uri.parse(uriStr);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    private class GPSLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location){
            _latitude = location.getLatitude();
            _longitude = location.getLongitude();
            _tvLatitude.setText(Double.toString(_latitude));
            _tvLongitude.setText(Double.toString(_longitude));
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras){}

        @Override
        public void onProviderEnabled(String provider){}

        @Override
        public void onProviderDisabled(String provider){}
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
        if(requestCode == 1000 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            GPSLocationListener locationListener = new GPSLocationListener();
            if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
                return;
            }
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
    }

}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.implicintintentsample">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

strings.xml


<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <string name="app_name">Map app</string>
    <string name="bt_map_search">Map search</string>
    <string name="tv_current_title">Current location</string>
    <string name="tv_latitude_title">latitude:</string>
    <string name="tv_longitude_title">longitude:</string>
    <string name="bt_map_current">Map display</string>
</resources>

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <EditText
            android:id="@+id/etSearchWord"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType=""
            android:autofillHints="" />

        <Button
            android:id="@+id/btMapSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onMapSearchButtonClick"
            android:text="@string/bt_map_search"
            />
    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/tv_current_title"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/tv_latitude_title"
            />

        <TextView
            android:id="@+id/tvLatitude"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="0.5"
            android:maxLines="1"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/tv_longitude_title"
            />

        <TextView
            android:id="@+id/tvLongitude"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:maxLines="1"
            />

        <Button
            android:id="@+id/btMapShowCurrent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onMapShowCurrentButtonClick"
            android:text="@string/bt_map_current"
            />
    </LinearLayout>
</LinearLayout>

Recommended Posts

I tried to create a simple map app in Android Studio
I tried to create a LINE clone app
I tried to create a Clova skill in Java
[Rails] I tried to create a mini app with FullCalendar
java I tried to break a simple block
I tried to create Alexa skill in Java
I tried to make a simple face recognition Android application using OpenCV
I tried using a database connection in Android development
I tried to decorate the simple calendar a little
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
I got a cannot resolve symbol in Android Studio
I tried to make a login function in Java
[Azure] I tried to create a Java application for free-Web App creation- [Beginner]
I tried to create a java8 development environment with Chocolatey
I tried adding a separator line to TabLayout on Android
I tried to convert a string to a LocalDate type in Java
I want to create a Parquet file even in Ruby
I tried to implement a buggy web application in Kotlin
I tried to make a client of RESAS-API in Java
I tried to create a padrino development environment with Docker
Create a new app in Rails
Try to create a server-client app
I made a matching app (Android app)
[Android] I made a pedometer app.
How to create a Wear OS by Google app project on Android Studio 3.0 or higher
Create a clear time ranking in Firebase's Realtime Database (Android app)
I want to create a chat screen for the Swift chat app!
I tried to illuminate the Christmas tree in a life game
I tried to write code like a type declaration in Ruby
I tried to create a Spring MVC development environment on Mac
I tried to build a simple application using Dockder + Rails Scaffold
I tried a calendar problem in Ruby
I tried to introduce CircleCI 2.0 to Rails app
Create a TODO app in Java 7 Create Header
I made a calculator app on Android
I tried to take a look at the flow of Android development environment construction with Android Studio
I made a rock-paper-scissors app with android
Java to C and C to Java in Android Studio
I have a question about Android studio.
I tried embedding a formula in Javadoc
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
How to use ExpandableListView in Android Studio
I tried to make a parent class of a value object in Ruby
I tried to build a Firebase application development environment with Docker in 2020
I thought about the best way to create a ValueObject in Ruby
I tried to make a talk application in Java using AI "A3RT"
Steps to create a simple camel app using Apache Camel Spring Boot starters
Riot (chat app) development (settings) in Android Studio
A note about adding Junit 4 to Android Studio
Create a simple search app with Spring Boot
I tried to implement polymorphic related in Nogizaka.
[Android] Inherit ImageView to create a new class
I tried to organize the session in Rails
3 ways to import the library in Android Studio
Create a simple batch processing framework in Eclipse.
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
Try to create a bulletin board in Java
I tried to implement deep learning in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
How to create a theme in Liferay 7 / DXP