1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| package com.example.a7map;
import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.fragment.app.FragmentActivity;
import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast;
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.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import com.example.a7map.databinding.ActivityMapsBinding;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap; private ActivityMapsBinding binding;
private static final int REQUEST_ID_LOCATION_PERMISSION = 99;
TextView textLat; TextView textLong; LocationManager locationManager; LocationListener locationListener;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
binding = ActivityMapsBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); //let us know the map is ready mapFragment.getMapAsync(this);
askLocationPermission();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationListener = new mylocationlistener(); }
/** * 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 public void onMapReady(GoogleMap googleMap) { mMap = googleMap;
// Add a marker in Sydney and move the camera LatLng Edinburgh = new LatLng(55.953251, -3.188267); //add new markers mMap.addMarker(new MarkerOptions().position(Edinburgh).title("Marker in Edinburgh")); //move the center of camera to marker mMap.moveCamera(CameraUpdateFactory.newLatLng(Edinburgh));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat return; } //enable the location button mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); mMap.getUiSettings().setCompassEnabled(true); mMap.getUiSettings().setRotateGesturesEnabled(true); mMap.getUiSettings().setScrollGesturesEnabled(true); mMap.getUiSettings().setTiltGesturesEnabled(true); }
private void askLocationPermission() { if (android.os.Build.VERSION.SDK_INT >= 23) { //check if we have location permission int CoarseLocation = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION); int FinaLocationPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION); int internetPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET);
if (CoarseLocation != PackageManager.PERMISSION_GRANTED || internetPermission != PackageManager.PERMISSION_GRANTED || FinaLocationPermission != PackageManager.PERMISSION_GRANTED) { //if dont permitted yet, prompt user this.requestPermissions( new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET }, REQUEST_ID_LOCATION_PERMISSION ); } } }
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) { case REQUEST_ID_LOCATION_PERMISSION: { //if request cancelled, result array becomes empty if (grantResults.length > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "Permission granted!", Toast.LENGTH_LONG).show(); } //if cancelled or denied else { Toast.makeText(this, "Permission denied!", Toast.LENGTH_LONG).show(); }
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Toast.makeText(this, "Open GPS", Toast.LENGTH_SHORT).show(); }
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat return; } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); break; } } }
double tlat; double tlong; class mylocationlistener implements LocationListener{ @Override //called when GPS changes public void onLocationChanged(@NonNull Location location) { if (location != null) { tlat = location.getLatitude(); tlong = location.getLongitude(); textLat.setText(Double.toString(tlat)); textLong.setText(Double.toString(tlong)); Toast.makeText(MapsActivity.this,"New Location "+String.valueOf(tlat)+" "+String.valueOf(tlong),Toast.LENGTH_LONG).show(); updateMap(); } } }
//add a new point marker and move camera to that place private void updateMap(){ //Add a marker in the Edinburgh and move the camera LatLng latLng = new LatLng(tlat,tlong); mMap.addMarker(new MarkerOptions().position(latLng).title("New location")); //Move tie map camera to the new location mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); }
private void setUpMapIfNeeded(){ //null check if(mMap==null){ SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } }
@Override protected void onPause() { super.onPause(); }
@Override protected void onResume() { super.onResume();
setUpMapIfNeeded(); } }
|