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