地図に名前と、緯度経度を入れて実行できるようにしました。
良ければ確認お願いします。
This commit is contained in:
oKikuti 2024-07-03 14:13:06 +09:00
parent 9dec1476e1
commit 11ea004d30
3 changed files with 70 additions and 33 deletions

View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">

View File

@ -168,12 +168,14 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
classId = CreateUUID.generateUUID(classIdList); classId = CreateUUID.generateUUID(classIdList);
imageUuid.setImageResource(R.drawable.checked_image);
Toast.makeText(MainActivity.this, "クラスID: " + classId, Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, "クラスID: " + classId, Toast.LENGTH_SHORT).show();
} }
}); });
builder.setNegativeButton("No", new DialogInterface.OnClickListener() { builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
imageUuid.setImageResource(R.drawable.checked_image);
Log.d("DialogNO", "DialogでNoが選ばれました"); Log.d("DialogNO", "DialogでNoが選ばれました");
} }
}); });

View File

@ -1,7 +1,9 @@
package com.example.oplogy; package com.example.oplogy;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.webkit.WebSettings; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
@ -14,6 +16,8 @@ import androidx.fragment.app.FragmentActivity;
import com.example.oplogy.databinding.MapsBinding; import com.example.oplogy.databinding.MapsBinding;
import java.util.Random;
public class Maps extends FragmentActivity implements View.OnClickListener { public class Maps extends FragmentActivity implements View.OnClickListener {
private WebView webView; private WebView webView;
@ -37,53 +41,85 @@ public class Maps extends FragmentActivity implements View.OnClickListener {
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient()); webView.setWebViewClient(new WebViewClient());
// ここにデータを入れておいてください処理は[/]で区切っています(緯度と経度で入れているのでそこで一度試してください) // 初期の緯度経度と名前のセットをロード
loadMapInWebView("35.09050879999539,136.87845379325216,名古屋港水族館/35.091950716938875,136.8826598363985,2番目/35.09273643623442,136.88154941341296,3番目"); loadMapInWebView("35.09050879999539,136.87845379325216/35.091950716938875,136.8826598363985/35.09273643623442,136.88154941341296");
loadName("名古屋港水族館/2番目/3番目");
} }
// WebViewの処理ですMapの中の処理をやっています // WebViewの処理ですMapの中の処理をやっています
private void loadMapInWebView(String locations) { private void loadMapInWebView(String locations) {
// 区切ることで追加の地点を入れて最終地点にピンを打ってある状態です try {
String[] locArray = locations.split("/"); String[] locArray = locations.split("/");
StringBuilder urlBuilder = new StringBuilder("https://www.google.com/maps/dir/?api=1&travelmode=driving");
// 各地点の名前をScrollViewに表示 if (locArray.length > 0) {
for (String loc : locArray) { urlBuilder.append("&origin=").append(locArray[0]);
String[] parts = loc.split(",");
if (parts.length == 3) {
addLocationToScrollView(parts[2]);
}
}
// URLで経路案内での表示をしています if (locArray.length > 1) {
StringBuilder urlBuilder = new StringBuilder("https://www.google.com/maps/dir/?api=1&travelmode=driving"); urlBuilder.append("&destination=").append(locArray[locArray.length - 1]);
if (locArray.length > 0) { if (locArray.length > 2) {
urlBuilder.append("&origin=").append(locArray[0]); urlBuilder.append("&waypoints=");
for (int i = 1; i < locArray.length - 1; i++) {
if (locArray.length > 1) { urlBuilder.append(locArray[i]);
urlBuilder.append("&destination=").append(locArray[locArray.length - 1]); if (i < locArray.length - 2) {
urlBuilder.append("|");
if (locArray.length > 2) { }
urlBuilder.append("&waypoints=");
for (int i = 1; i < locArray.length - 1; i++) {
urlBuilder.append(locArray[i]);
if (i < locArray.length - 2) {
urlBuilder.append("|");
} }
} }
} }
} }
}
webView.loadUrl(urlBuilder.toString()); runOnUiThread(() -> webView.loadUrl(urlBuilder.toString()));
} catch (Exception e) {
Log.e("Maps", "Error loading map in WebView", e);
}
} }
// 新しい名前を追加するメソッドです
private void loadName(String locNames) {
try {
String[] locArray = locNames.split("/");
for (String loc : locArray) {
runOnUiThread(() -> addLocationToScrollView(loc));
}
} catch (Exception e) {
Log.e("Maps", "Error loading names", e);
}
}
// ScrollViewに新しい場所の名前を追加するメソッドです
private void addLocationToScrollView(String locationName) { private void addLocationToScrollView(String locationName) {
TextView textView = new TextView(this); runOnUiThread(() -> {
textView.setText(locationName); try {
textView.setTextSize(16); TextView textView = new TextView(this);
textView.setPadding(16, 16, 16, 16); textView.setText(locationName);
locationsName.addView(textView); textView.setTextSize(16);
textView.setPadding(16, 16, 16, 16);
// ランダムな背景色を設定
Random random = new Random();
int color = Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256));
textView.setBackgroundColor(color);
// 下線のViewを追加
View underline = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
2 // 下線の高さをピクセル単位で設定
);
params.setMargins(0, 0, 0, 16); // 下線の上下のマージンを設定
underline.setLayoutParams(params);
underline.setBackgroundColor(Color.BLACK); // 下線の色を設定
// TextViewと下線のViewを追加
locationsName.addView(textView);
locationsName.addView(underline);
} catch (Exception e) {
Log.e("Maps", "Error adding location to ScrollView", e);
}
});
} }
@Override @Override