菊池 #38

Merged
Utahshi merged 5 commits from AlsoMaps into master 2024-07-08 07:58:12 +00:00
4 changed files with 58 additions and 232 deletions
Showing only changes of commit 5599ff7b1d - Show all commits

View File

@ -5,8 +5,11 @@ import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.CameraUpdateFactory;
@ -38,12 +41,11 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
private GoogleMap mMap; private GoogleMap mMap;
private MapsBinding binding; private MapsBinding binding;
private LinearLayout locationsName; private LinearLayout locationsName;
private Spinner date;
private static final int[] COLORS = new int[]{ private static final int[] COLORS = new int[]{
Color.BLUE, Color.RED, Color.GREEN, Color.MAGENTA, Color.CYAN, Color.YELLOW Color.BLUE, Color.RED, Color.GREEN, Color.MAGENTA, Color.CYAN, Color.YELLOW
}; };
private int colorIndex = 0; private int colorIndex = 0;
private List<LatLng> latLngList = new ArrayList<>(); private List<LatLng> latLngList = new ArrayList<>();
private List<String> nameList = new ArrayList<>(); private List<String> nameList = new ArrayList<>();
private List<Integer> colorList = new ArrayList<>(); private List<Integer> colorList = new ArrayList<>();
@ -63,27 +65,68 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
backMain.setOnClickListener(this); backMain.setOnClickListener(this);
locationsName = findViewById(R.id.locationsName); locationsName = findViewById(R.id.locationsName);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("6月3日");
adapter.add("6月4日");
adapter.add("6月5日");
date = findViewById(R.id.date);
date.setAdapter(adapter);
date.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
if (selectedItem.equals("6月4日")) {
secondMapAndNames();
} else if (selectedItem.equals("6月5日")) {
thildMapAndNames();
} else {
firstMapAndNames();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing
}
});
} }
// 書くところはここのみです後は触らなくて大丈夫です
@Override @Override
public void onMapReady(GoogleMap googleMap) { public void onMapReady(GoogleMap googleMap) {
mMap = googleMap; mMap = googleMap;
mMap.setOnMarkerClickListener(this); mMap.setOnMarkerClickListener(this);
firstMapAndNames();
}
// ここに緯度と経度を入れてください/で区切って取得する処理となっています private void firstMapAndNames() {
String locationData = "35.09050879999539,136.87845379325216/35.091950716938875,136.8826598363985/35.09273643623442,136.88154941341296/35.09473643623442,136.88154941341296/35.09673643623442,136.88154941341296"; 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番目"; String nameData = "名古屋港水族館/2番目/3番目/4番目/5番目";
loadMapAndNames(locationData, nameData); loadMapAndNames(locationData, nameData);
} }
private void secondMapAndNames() {
String locationData2 = "35.09050879999539,136.87845379325216/35.091950716938875,136.8826598363985/35.09273643623442,136.88154941341296";
String nameData2 = "名古屋港水族館/2番目/3番目";
loadMapAndNames(locationData2, nameData2);
}
private void thildMapAndNames() {
String locationData3 = "35.09050879999539,136.87845379325216/35.091950716938875,136.8826598363985";
String nameData3 = "名古屋港水族館/2番目";
loadMapAndNames(locationData3, nameData3);
}
// 名前入力の処理
private void loadMapAndNames(String locationData, String nameData) { private void loadMapAndNames(String locationData, String nameData) {
try { try {
latLngList.clear();
nameList.clear();
colorList.clear();
locationsName.removeAllViews();
mMap.clear();
String[] locArray = locationData.split("/"); String[] locArray = locationData.split("/");
String[] nameArray = nameData.split("/"); String[] nameArray = nameData.split("/");
@ -115,7 +158,6 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
} }
} }
//ルート表示の処理APIがちゃんと作動している時に動く所です
private void drawRoute() { private void drawRoute() {
new Thread(() -> { new Thread(() -> {
try { try {
@ -146,7 +188,7 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
} }
br.close(); br.close();
Log.d("Maps", "API response: " + jsonResults.toString()); // APIレスポンス全体をログに記録 Log.d("Maps", "API response: " + jsonResults.toString());
JsonObject jsonObject = new Gson().fromJson(jsonResults.toString(), JsonObject.class); JsonObject jsonObject = new Gson().fromJson(jsonResults.toString(), JsonObject.class);
JsonArray routes = jsonObject.getAsJsonArray("routes"); JsonArray routes = jsonObject.getAsJsonArray("routes");
@ -158,11 +200,8 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
Log.d("Maps", "Polyline points: " + points); Log.d("Maps", "Polyline points: " + points);
runOnUiThread(() -> { runOnUiThread(() -> mMap.addPolyline(new PolylineOptions().addAll(points).width(5).color(Color.BLUE)));
mMap.addPolyline(new PolylineOptions().addAll(points).width(5).color(Color.BLUE));
});
} else { } else {
// エラーが発生した場合の処理
JsonPrimitive errorMessage = jsonObject.getAsJsonPrimitive("error_message"); JsonPrimitive errorMessage = jsonObject.getAsJsonPrimitive("error_message");
if (errorMessage != null) { if (errorMessage != null) {
Log.e("Maps", "Error: " + errorMessage.getAsString()); Log.e("Maps", "Error: " + errorMessage.getAsString());
@ -170,7 +209,6 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
Log.e("Maps", "No routes found and no error message provided"); Log.e("Maps", "No routes found and no error message provided");
} }
} }
} catch (Exception e) { } catch (Exception e) {
Log.e("Maps", "Error drawing route", e); Log.e("Maps", "Error drawing route", e);
} }
@ -209,8 +247,6 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
return poly; return poly;
} }
// マップのピン等に入れる色の処理
private float getHueFromColor(int color) { private float getHueFromColor(int color) {
float[] hsv = new float[3]; float[] hsv = new float[3];
Color.colorToHSV(color, hsv); Color.colorToHSV(color, hsv);
@ -228,8 +264,6 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
} }
} }
// ScrollViewのTextViewの中の処理名前や色等
private void addLocationToScrollView(String locationName, int color) { private void addLocationToScrollView(String locationName, int color) {
runOnUiThread(() -> { runOnUiThread(() -> {
try { try {
@ -256,14 +290,12 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
}); });
} }
//色の重複赤等を避ける処理
private int getNextColor() { private int getNextColor() {
int color = COLORS[colorIndex]; int color = COLORS[colorIndex];
colorIndex = (colorIndex + 1) % COLORS.length; colorIndex = (colorIndex + 1) % COLORS.length;
return color; return color;
} }
//クリックしたらメイン画面に戻ります
@Override @Override
public void onClick(View view) { public void onClick(View view) {
if (view.getId() == R.id.BackMain) { if (view.getId() == R.id.BackMain) {
@ -271,8 +303,6 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
} }
} }
//マップのピンをクリックしたときの処理
@Override @Override
public boolean onMarkerClick(Marker marker) { public boolean onMarkerClick(Marker marker) {
String locationName = (String) marker.getTag(); String locationName = (String) marker.getTag();

View File

@ -1,149 +0,0 @@
package com.example.oplogy;
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.MarkerOptions;
import com.example.oplogy.databinding.MapsBinding;
import com.google.android.gms.maps.model.PolylineOptions;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MapsFragment extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener {
ImageView backMain;
private GoogleMap mMap;
private MapsBinding binding;
private LinearLayout locationsName;
Random random = new Random();
static int[] colors = new int[]{
Color.BLUE, Color.RED, Color.GREEN, Color.MAGENTA, Color.CYAN, Color.YELLOW
};
private List<LatLng> latLngList = new ArrayList<>();
private List<String> nameList = new ArrayList<>();
private List<Integer> colorList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
locationsName = findViewById(R.id.locationsName); // locationsNameの初期化
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
String locationData = "35.09050879999539,136.87845379325216/35.091950716938875,136.8826598363985/35.09273643623442,136.88154941341296";
String nameData = "名古屋港水族館/2番目/3番目";
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 = colors[random.nextInt(colors.length)];
colorList.add(color);
addPinToMap(name, position, color);
addLocationToScrollView(name, color);
}
}
if (!latLngList.isEmpty()) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLngList.get(0), 17));
mMap.addPolyline(new PolylineOptions().addAll(latLngList).width(5).color(Color.BLUE));
}
} catch (Exception e) {
Log.e("Maps", "Error loading maps and names", e);
}
}
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) {
mMap.addMarker(new MarkerOptions()
.position(position)
.title(locationName)
.icon(BitmapDescriptorFactory.defaultMarker(getHueFromColor(color)))
);
}
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 to ScrollView", e);
}
});
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.BackMain) {
finish();
}
}
}

View File

@ -1,54 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical"
tools:context=".MapsFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="10">
<ImageView
android:id="@+id/BackMain"
android:layout_width="40dp"
android:layout_height="90dp"
android:src="@drawable/back_button"
android:layout_weight="5" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="今日の日付"
android:textSize="40dp" />
</LinearLayout>
<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"
tools:context=".Maps" />
<ScrollView
android:id="@+id/scrollable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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

@ -20,15 +20,14 @@
android:layout_width="40dp" android:layout_width="40dp"
android:layout_height="90dp" android:layout_height="90dp"
android:src="@drawable/back_button" android:src="@drawable/back_button"
android:layout_weight="5" /> android:layout_weight="4" />
<TextView <Spinner
android:id="@+id/date" android:id="@+id/date"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="60dp"
android:layout_weight="4" android:layout_weight="5"
android:hint="今日の日付" android:textSize="50dp" />
android:textSize="40dp" />
</LinearLayout> </LinearLayout>
<fragment <fragment