菊池
取り敢えず3日分でテストしてみました。 別々の値で入れていますので、問題ないかと思います。
This commit is contained in:
parent
20a5100a6a
commit
5599ff7b1d
|
@ -5,8 +5,11 @@ import android.graphics.Color;
|
|||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.google.android.gms.maps.CameraUpdateFactory;
|
||||
|
@ -38,12 +41,11 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
|
|||
private GoogleMap mMap;
|
||||
private MapsBinding binding;
|
||||
private LinearLayout locationsName;
|
||||
|
||||
private Spinner date;
|
||||
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<>();
|
||||
|
@ -63,27 +65,68 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
|
|||
backMain.setOnClickListener(this);
|
||||
|
||||
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
|
||||
public void onMapReady(GoogleMap googleMap) {
|
||||
mMap = googleMap;
|
||||
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 nameData = "名古屋港水族館/2番目/3番目/4番目/5番目";
|
||||
|
||||
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) {
|
||||
try {
|
||||
latLngList.clear();
|
||||
nameList.clear();
|
||||
colorList.clear();
|
||||
locationsName.removeAllViews();
|
||||
mMap.clear();
|
||||
|
||||
String[] locArray = locationData.split("/");
|
||||
String[] nameArray = nameData.split("/");
|
||||
|
||||
|
@ -115,7 +158,6 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
|
|||
}
|
||||
}
|
||||
|
||||
//ルート表示の処理(APIがちゃんと作動している時に動く所です)
|
||||
private void drawRoute() {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
|
@ -146,7 +188,7 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
|
|||
}
|
||||
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);
|
||||
JsonArray routes = jsonObject.getAsJsonArray("routes");
|
||||
|
@ -158,11 +200,8 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
|
|||
|
||||
Log.d("Maps", "Polyline points: " + points);
|
||||
|
||||
runOnUiThread(() -> {
|
||||
mMap.addPolyline(new PolylineOptions().addAll(points).width(5).color(Color.BLUE));
|
||||
});
|
||||
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());
|
||||
|
@ -170,7 +209,6 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
|
|||
Log.e("Maps", "No routes found and no error message provided");
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e("Maps", "Error drawing route", e);
|
||||
}
|
||||
|
@ -209,8 +247,6 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
|
|||
return poly;
|
||||
}
|
||||
|
||||
|
||||
// マップのピン等に入れる色の処理
|
||||
private float getHueFromColor(int color) {
|
||||
float[] hsv = new float[3];
|
||||
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) {
|
||||
runOnUiThread(() -> {
|
||||
try {
|
||||
|
@ -256,14 +290,12 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
|
|||
});
|
||||
}
|
||||
|
||||
//色の重複(赤・赤等を避ける)処理
|
||||
private int getNextColor() {
|
||||
int color = COLORS[colorIndex];
|
||||
colorIndex = (colorIndex + 1) % COLORS.length;
|
||||
return color;
|
||||
}
|
||||
|
||||
//クリックしたらメイン画面に戻ります
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if (view.getId() == R.id.BackMain) {
|
||||
|
@ -271,8 +303,6 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.O
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
//マップのピンをクリックしたときの処理
|
||||
@Override
|
||||
public boolean onMarkerClick(Marker marker) {
|
||||
String locationName = (String) marker.getTag();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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>
|
||||
|
|
@ -20,15 +20,14 @@
|
|||
android:layout_width="40dp"
|
||||
android:layout_height="90dp"
|
||||
android:src="@drawable/back_button"
|
||||
android:layout_weight="5" />
|
||||
android:layout_weight="4" />
|
||||
|
||||
<TextView
|
||||
<Spinner
|
||||
android:id="@+id/date"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="4"
|
||||
android:hint="今日の日付"
|
||||
android:textSize="40dp" />
|
||||
android:layout_height="60dp"
|
||||
android:layout_weight="5"
|
||||
android:textSize="50dp" />
|
||||
</LinearLayout>
|
||||
|
||||
<fragment
|
||||
|
|
Loading…
Reference in New Issue
Block a user