マップでDirectionの表示をするコードは入れました。(機能はしていません)
できればDirectionAPIのキーをコメントで指定している部分に入れておくか、自分に送ってくれると助かります。もしできなかった場合はすぐに言ってください
お願いします
This commit is contained in:
oKikuti 2024-06-28 11:28:40 +09:00
parent 9bad03da2d
commit b77c78f9dd
5 changed files with 76 additions and 62 deletions

View File

@ -4,6 +4,7 @@
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="testRunner" value="GRADLE" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="jbr-17" /> <option name="gradleJvm" value="jbr-17" />
<option name="modules"> <option name="modules">

View File

@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<!-- インターネットへのアクセス権限 -->
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<application <application

View File

@ -88,6 +88,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
// クリック処理 // クリック処理
@Override @Override
public void onClick(View view) { public void onClick(View view) {
// ID作成のクリック処理 // ID作成のクリック処理
if(view == creatUUID){ if(view == creatUUID){
imageUuid.setImageResource(R.drawable.ischecked_uuid); imageUuid.setImageResource(R.drawable.ischecked_uuid);
@ -101,13 +102,15 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
if(view == setUp){ if(view == setUp){
imageSetup.setImageResource(R.drawable.ischecked_uuid); imageSetup.setImageResource(R.drawable.ischecked_uuid);
Intent toSetup = new Intent(MainActivity.this,SetUpActivity.class); Intent toSetup = new Intent(MainActivity.this,SetUpActivity.class);
startActivity(toSetup); startActivityForResult(toSetup,100);
overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
finish(); // 画面遷移後元の状態に戻す finish(); // 画面遷移後元の状態に戻す
} }
if (view == imageSetup){ if (view == imageSetup){
imageSetup.setImageResource(R.drawable.ischecked_uuid); imageSetup.setImageResource(R.drawable.ischecked_uuid);
Intent toSetup = new Intent(MainActivity.this,SetUpActivity.class); Intent toSetup = new Intent(MainActivity.this,SetUpActivity.class);
startActivity(toSetup); startActivityForResult(toSetup,100);
overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
finish(); // 画面遷移後元の状態に戻す finish(); // 画面遷移後元の状態に戻す
} }
@ -152,6 +155,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
Log.d("DialogNO", "DialogでNoが選ばれました"); Log.d("DialogNO", "DialogでNoが選ばれました");
// Noを押したら元の画像に戻す処理
imageUuid.setImageResource(R.drawable.checked_image);
} }
}); });

View File

@ -1,52 +1,49 @@
package com.example.oplogy; package com.example.oplogy;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.media.Image; import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment; 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.LatLng;
import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.MarkerOptions;
import com.example.oplogy.databinding.MapsBinding; import com.example.oplogy.databinding.MapsBinding;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions; import com.google.android.gms.maps.model.PolylineOptions;
import com.google.maps.DirectionsApi; import com.google.maps.DirectionsApi;
import com.google.maps.DirectionsApiRequest;
import com.google.maps.GeoApiContext; import com.google.maps.GeoApiContext;
import com.google.maps.model.DirectionsResult; import com.google.maps.model.DirectionsResult;
import com.google.maps.model.DirectionsRoute;
import com.google.maps.model.TravelMode;
import java.util.Locale; import java.util.ArrayList;
import java.util.Map; import java.util.List;
public class Maps extends FragmentActivity implements OnMapReadyCallback,View.OnClickListener{ public class Maps extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener, LocationListener {
// ボタンの戻る処理 private static final String TAG = "MapsActivity";
ImageView backMain; ImageView backMain;
private GoogleMap mMap; private GoogleMap mMap;
private MapsBinding binding; private MapsBinding binding;
private LatLng loc;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
binding = MapsBinding.inflate(getLayoutInflater()); binding = MapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot()); setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map); .findFragmentById(R.id.map);
mapFragment.getMapAsync(this); mapFragment.getMapAsync(this);
@ -55,66 +52,73 @@ public class Maps extends FragmentActivity implements OnMapReadyCallback,View.On
backMain.setOnClickListener(this); backMain.setOnClickListener(this);
} }
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override @Override
public void onMapReady(GoogleMap googleMap) { public void onMapReady(GoogleMap googleMap) {
mMap = googleMap; mMap = googleMap;
/// 地図の倍率を指定 LatLng startPoint = new LatLng(35.09050879999539, 136.87845379325216);
LatLng endPoint = new LatLng(35.09364708442631, 136.88171563326418);
// ここに地点の処理を書いておく addNumberedMarker(startPoint, "1: 名古屋港水族館");
// Add a marker in Sydney and move the camera addNumberedMarker(new LatLng(35.09284820618655, 136.88165119390393), "2: Waypoint");
loc = new LatLng(35.09050879999539, 136.87845379325216); addNumberedMarker(endPoint, "3: Destination");
mMap.addMarker(new MarkerOptions().position(loc).title("名古屋港水族館"));
/// 表示位置を地図に指定
mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 17)); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(startPoint, 17));
LatLng startLatLng = new LatLng(35.09050879999539, 136.87845379325216); // Directions API リクエスト
LatLng secandLatLng = new LatLng(35.09284820618655, 136.88165119390393); new Thread(() -> {
LatLng thirdLatLng = new LatLng(35.09364708442631, 136.88171563326418); try {
DirectionsResult result = DirectionsApi.newRequest(new GeoApiContext.Builder()
.apiKey("AIzaSyBQ1Ak-I2NL5TP4K59ZI0VgzKk6HNZuusw") // DirectionsAPIキーの取得
.build())
.mode(TravelMode.DRIVING)
.origin(new com.google.maps.model.LatLng(startPoint.latitude, startPoint.longitude))
.destination(new com.google.maps.model.LatLng(endPoint.latitude, endPoint.longitude))
.await();
if (result.routes != null && result.routes.length > 0) {
DirectionsRoute route = result.routes[0];
Marker startMaker = mMap.addMarker(new MarkerOptions() List<LatLng> decodedPath = new ArrayList<>();
.position(startLatLng) for (com.google.maps.model.LatLng point : route.overviewPolyline.decodePath()) {
.title("名古屋港水族館") decodedPath.add(new LatLng(point.lat, point.lng));
); }
Marker secondMaker = mMap.addMarker(new MarkerOptions() runOnUiThread(() -> {
.position(secandLatLng) PolylineOptions polylineOptions = new PolylineOptions().addAll(decodedPath).color(Color.BLUE).width(10);
.title("2番目") mMap.addPolyline(polylineOptions);
);
Marker thirdMaker = mMap.addMarker(new MarkerOptions() mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
.position(thirdLatLng) LatLngBounds.builder().include(startPoint).include(endPoint).build(), 100));
.title("3番目") });
); } else {
Log.e(TAG, "No routes found");
drowRoute(startLatLng,secandLatLng,thirdLatLng); }
} catch (Exception ex) {
Log.e(TAG, "Error fetching directions", ex);
}
}).start();
} }
private void drowRoute(LatLng startLatLng,LatLng secondLatLung,LatLng thirdLatLng){ // ここで入れている地点に番号を振っている
GeoApiContext context = new GeoApiContext.Builder() private void addNumberedMarker(LatLng position, String title) {
.apiKey("AIzaSyBQ1Ak-I2NL5TP4K59ZI0VgzKk6HNZuusw") mMap.addMarker(new MarkerOptions().position(position).title(title));
.build();
} }
// クリックするとメイン画面に戻る処理
@Override @Override
public void onClick(View view) { public void onClick(View view) {
if(view == backMain){ if (view == backMain) {
Intent backMain = new Intent(Maps.this,MainActivity.class); Intent backMain = new Intent(Maps.this, MainActivity.class);
startActivity(backMain); startActivity(backMain);
} }
} }
}
// LocationListenerの呼び出し時に実装するもの
@Override
public void onLocationChanged(@NonNull Location location) {
}
}

View File

@ -89,7 +89,8 @@ public class SetUpActivity extends FragmentActivity
toMain.setOnClickListener(view -> { toMain.setOnClickListener(view -> {
Intent intent = new Intent(SetUpActivity.this,MainActivity.class); //main画面へ戻る処理 Intent intent = new Intent(SetUpActivity.this,MainActivity.class); //main画面へ戻る処理
startActivity(intent); startActivityForResult(intent,100);
overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
}); });
setUp.setOnClickListener(view -> { setUp.setOnClickListener(view -> {