ピンと色が一致するようになり、ピンをクリックと名前などが見えるように設定しました。
Main画面でUUID作成を押したときに、(Yes/No)を押してもチェックが消えなかったのでそこの訂正もしました。
そこの変更も確認お願いします。
This commit is contained in:
oKikuti 2024-07-07 22:08:44 +09:00
parent 8a23bd3f8a
commit 1b6e22f677
8 changed files with 283 additions and 53 deletions

View File

@ -6,7 +6,7 @@
<GradleProjectSettings>
<option name="testRunner" value="GRADLE" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="Embedded JDK" />
<option name="gradleJvm" value="jbr-17" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />

View File

@ -52,3 +52,11 @@ dependencies {
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
}
//Map用の依存関係の追加
dependencies {
implementation 'com.google.android.gms:play-services-maps:18.0.0'
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'com.google.code.gson:gson:2.8.9' // Gsonライブラリの追加
}

View File

@ -3,6 +3,9 @@
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
tools:ignore="CoarseFineLocation" />
<application
android:allowBackup="true"
@ -31,7 +34,7 @@
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBQ1Ak-I2NL5TP4K59ZI0VgzKk6HNZuusw" />
android:value="AIzaSyBqLqPm76UFpwbpU8b3oiH0wbkhosrPWpU" />
<activity
android:name=".Maps"

View File

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

View File

@ -1,22 +1,52 @@
package com.example.oplogy;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import androidx.fragment.app.FragmentActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.example.oplogy.databinding.MapsBinding;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
public class Maps extends FragmentActivity implements View.OnClickListener {
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Maps extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener, GoogleMap.OnMarkerClickListener {
private WebView webView;
ImageView backMain;
private GoogleMap mMap;
private MapsBinding binding;
private LinearLayout locationsName;
private static final int[] COLORS = new int[]{
Color.BLUE, Color.RED, Color.GREEN, Color.MAGENTA, Color.CYAN, Color.YELLOW
};
private int colorIndex = 0;
private List<LatLng> latLngList = new ArrayList<>();
private List<String> nameList = new ArrayList<>();
private List<Integer> colorList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -25,51 +55,231 @@ public class Maps extends FragmentActivity implements View.OnClickListener {
binding = MapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
backMain = findViewById(R.id.BackMain);
backMain.setOnClickListener(this);
webView = findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
// ここにデータを入れておいてください処理は[/]で区切っています
loadMapInWebView("35.09050879999539,136.87845379325216/35.09284820618655,136.88165119390393/35.09364708442631,136.88171563326418");
locationsName = findViewById(R.id.locationsName);
}
// WebViewの処理ですMapの中の処理をやっています
private void loadMapInWebView(String locations) {
// 区切ることで追加の地点を入れて最終地点にピンを打ってある状態です
String[] locArray = locations.split("/");
// URLで経路案内での表示をしています
StringBuilder urlBuilder = new StringBuilder("https://www.google.com/maps/dir/?api=1&travelmode=driving");
if (locArray.length > 0) {
urlBuilder.append("&origin=").append(locArray[0]);
// 書くところはここのみです後は触らなくて大丈夫です
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMarkerClickListener(this);
if (locArray.length > 1) {
urlBuilder.append("&destination=").append(locArray[locArray.length - 1]);
// ここに緯度と経度を入れてください/で区切って取得する処理となっています
String locationData = "35.09050879999539,136.87845379325216/35.091950716938875,136.8826598363985/35.09273643623442,136.88154941341296/35.09473643623442,136.88154941341296/35.09673643623442,136.88154941341296";
// ここに名前等いれたい情報を乗せてください
String nameData = "名古屋港水族館/2番目/3番目/4番目/5番目";
if (locArray.length > 2) {
loadMapAndNames(locationData, nameData);
}
// 名前入力の処理
private void loadMapAndNames(String locationData, String nameData) {
try {
String[] locArray = locationData.split("/");
String[] nameArray = nameData.split("/");
for (int i = 0; i < locArray.length; i++) {
String[] latLng = locArray[i].split(",");
if (latLng.length == 2) {
double latitude = Double.parseDouble(latLng[0]);
double longitude = Double.parseDouble(latLng[1]);
LatLng position = new LatLng(latitude, longitude);
latLngList.add(position);
String name = nameArray.length > i ? nameArray[i] : "Unknown";
nameList.add(name);
int color = getNextColor();
colorList.add(color);
addPinToMap(name, position, color);
addLocationToScrollView(name, color);
}
}
if (!latLngList.isEmpty()) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLngList.get(0), 17));
drawRoute();
}
} catch (Exception e) {
Log.e("Maps", "Error loading maps and names", e);
}
}
//ルート表示の処理APIがちゃんと作動している時に動く所です
private void drawRoute() {
new Thread(() -> {
try {
StringBuilder urlBuilder = new StringBuilder("https://maps.googleapis.com/maps/api/directions/json?");
urlBuilder.append("origin=").append(latLngList.get(0).latitude).append(",").append(latLngList.get(0).longitude);
urlBuilder.append("&destination=").append(latLngList.get(latLngList.size() - 1).latitude).append(",").append(latLngList.get(latLngList.size() - 1).longitude);
if (latLngList.size() > 2) {
urlBuilder.append("&waypoints=");
for (int i = 1; i < locArray.length - 1; i++) {
urlBuilder.append(locArray[i]);
if (i < locArray.length - 2) {
for (int i = 1; i < latLngList.size() - 1; i++) {
urlBuilder.append("via:").append(latLngList.get(i).latitude).append(",").append(latLngList.get(i).longitude);
if (i < latLngList.size() - 2) {
urlBuilder.append("|");
}
}
}
urlBuilder.append("&mode=driving");
urlBuilder.append("&key=").append(getString(R.string.maps_api_key));
URL url = new URL(urlBuilder.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr);
StringBuilder jsonResults = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
jsonResults.append(line);
}
br.close();
Log.d("Maps", "API response: " + jsonResults.toString()); // APIレスポンス全体をログに記録
JsonObject jsonObject = new Gson().fromJson(jsonResults.toString(), JsonObject.class);
JsonArray routes = jsonObject.getAsJsonArray("routes");
if (routes.size() > 0) {
JsonObject route = routes.get(0).getAsJsonObject();
JsonObject polyline = route.getAsJsonObject("overview_polyline");
String encodedString = polyline.get("points").getAsString();
List<LatLng> points = decodePoly(encodedString);
Log.d("Maps", "Polyline points: " + points);
runOnUiThread(() -> {
mMap.addPolyline(new PolylineOptions().addAll(points).width(5).color(Color.BLUE));
});
} else {
// エラーが発生した場合の処理
JsonPrimitive errorMessage = jsonObject.getAsJsonPrimitive("error_message");
if (errorMessage != null) {
Log.e("Maps", "Error: " + errorMessage.getAsString());
} else {
Log.e("Maps", "No routes found and no error message provided");
}
}
webView.loadUrl(urlBuilder.toString());
} catch (Exception e) {
Log.e("Maps", "Error drawing route", e);
}
}).start();
}
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
// マップのピン等に入れる色の処理
private float getHueFromColor(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
return hsv[0];
}
private void addPinToMap(String locationName, LatLng position, int color) {
Marker marker = mMap.addMarker(new MarkerOptions()
.position(position)
.title(locationName)
.icon(BitmapDescriptorFactory.defaultMarker(getHueFromColor(color)))
);
if (marker != null) {
marker.setTag(locationName);
}
}
// ScrollViewのTextViewの中の処理名前や色等
private void addLocationToScrollView(String locationName, int color) {
runOnUiThread(() -> {
try {
TextView textView = new TextView(this);
textView.setText(locationName);
textView.setTextSize(20);
textView.setPadding(16, 16, 16, 16);
textView.setBackgroundColor(color);
View underline = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
3
);
params.setMargins(0, 0, 0, 16);
underline.setLayoutParams(params);
underline.setBackgroundColor(Color.BLACK);
locationsName.addView(textView);
locationsName.addView(underline);
} catch (Exception e) {
Log.e("Maps", "Error adding location to scroll view", e);
}
});
}
//色の重複赤等を避ける処理
private int getNextColor() {
int color = COLORS[colorIndex];
colorIndex = (colorIndex + 1) % COLORS.length;
return color;
}
//クリックしたらメイン画面に戻ります
@Override
public void onClick(View view) {
if (view == backMain) {
Intent backMain = new Intent(Maps.this, MainActivity.class);
startActivity(backMain);
if (view.getId() == R.id.BackMain) {
finish();
}
}
//マップのピンをクリックしたときの処理
@Override
public boolean onMarkerClick(Marker marker) {
String locationName = (String) marker.getTag();
if (locationName != null) {
marker.setTitle(locationName);
marker.showInfoWindow();
}
return false;
}
}

View File

@ -72,6 +72,11 @@
</LinearLayout>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"/>
<!-- ルート表示のレイアウト-->
<LinearLayout
@ -102,11 +107,6 @@
android:layout_gravity="center"/>
</LinearLayout>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"/>
<!-- 提出状況のレイアウト-->
<LinearLayout

View File

@ -31,17 +31,24 @@
android:textSize="40dp" />
</LinearLayout>
<!-- Webの処理 -->
<WebView
android:id="@+id/webView"
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6" />
android:layout_weight="6"
tools:context=".Maps" />
<ScrollView
android:id="@+id/scrollable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" />
android:layout_weight="3">
<LinearLayout
android:id="@+id/locationsName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</LinearLayout>

View File

@ -1,5 +1,6 @@
<resources>
<string name="app_name">oplogy</string>
<string name="app_name">Oplogy</string>
<string name="maps_api_key">AIzaSyBqLqPm76UFpwbpU8b3oiH0wbkhosrPWpU</string>
<string name="title_activity_maps">MapsActivity</string>
<string name="setUp">設定</string>
<string name="reset">リセット</string>
@ -11,7 +12,6 @@
<string name="breakTime">休憩時間</string>
<string name="totalStudent">生徒の人数</string>
<string name="timeSet">時刻設定</string>
<string name="Back">🔙</string>
<string name="startpoint">住所</string>
<string name="tenMinute">10分</string>
<string name="fifteenMinute">15分</string>