GoogleMaps is left blank and longitude + latitude is not passed to text variables.

AndriodManifest:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="gavno.ssod22"> <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <permission android:name="com.example.barcodelibrary.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="com.example.barcodelibrary.permission.MAPS_RECEIVE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <!-- The following two permissions are not required to use Google Maps Android API v2, but are recommended. --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!-- The ACCESS_COARSE/FINE_LOCATION permissions are not required to use Google Maps Android API v2, but you must specify either coarse or fine location permissions for the 'MyLocation' functionality. --> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <!-- The API key for Google Maps-based APIs is defined as a string resource. (See the file "res/values/google_maps_api.xml"). Note that the API key is linked to the encryption key used to sign the APK. You need a different API key for each encryption key, including the release key that is used to sign the APK for publishing. You can define the keys for the debug and release targets in src/debug/ and src/release/. --> <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyA2hl650r71dx6CsprmCN8wZ5d3EsgzSFk" /> <activity android:name=".MapsActivity" android:label="@string/title_activity_maps"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

MapsActivity:

 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, TextToSpeech.OnInitListener { private GoogleMap mMap; TextView tvNET; TextView tvGPS; DBHelper dbHelper; List<PointLocation> listLocations; private LocationManager locationManager; public double CoordLat = 0; public double CoordLen = 0; public String LOG_TAG = ""; @Override protected void onCreate(Bundle savedInstanceState) { if(Build.VERSION.SDK_INT>=23&&!isPermissionGranted()){ requestPermissions(PERMISSIONS,PERMISSION_ALL);} super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); tvNET = (TextView)findViewById(R.id.tvNET); tvGPS = (TextView)findViewById(R.id.tvGPS); dbHelper = new DBHelper(this); SQLiteDatabase db = dbHelper.getWritableDatabase(); Cursor c = db.query("location", null, null, null, null, null, null); if (c!=null){ if (c.moveToFirst()){ String[] str = new String[5]; do { for (String cn : c.getColumnNames()){ int i =c.getColumnIndex(cn); str[i] =c.getString(i); Log.d(LOG_TAG, str[i]); } listLocations.add(new PointLocation (Integer.valueOf(str[0]),str[3], Double.valueOf(str[1]),Double.valueOf(str[2]),str[4])); } while (c.moveToNext()); } c.close(); } else Log.d(LOG_TAG, "Cursor is null"); dbHelper.close(); mTTS = new TextToSpeech(this,this); } @Override public void onInit (int status){ if (status == TextToSpeech.SUCCESS){ Locale locale = new Locale("ru"); int result = mTTS.setLanguage(locale); if (result== TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){ Log.e("TTS","Извините, этот язык не поддерживается"); } } else { Log.e("TTS","Ошибка!"); } } @Override public void onDestroy(){ if (mTTS != null){ mTTS.stop(); mTTS.shutdown(); } super.onDestroy(); } private TextToSpeech mTTS; private boolean isPermissionGranted(){ if(checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION)== PackageManager.PERMISSION_GRANTED|| checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){ Log.v("MyLog","Permission granded"); return true; }else{Log.v("MyLog","Permissions not granded"); return false;} } /** * 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 sydney = new LatLng(CoordLat,CoordLen); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); } private void checkEnabled(){ tvGPS.setText("Доступность: " + locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)); tvNET.setText("Доступность: " + locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)); } private LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { showLocation(location); //Условие если вне радиуса точки то производится код ниже //mTTS.speak("Текст для воспроизведения", TextToSpeech.QUEUE_FLUSH, null); } @Override public void onProviderEnabled(String provider) { checkEnabled(); showLocation(locationManager.getLastKnownLocation(provider)); } @Override public void onProviderDisabled(String provider) { checkEnabled(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { if (provider.equals(LocationManager.GPS_PROVIDER)){ tvGPS.setText("Status: " + String.valueOf(status)); }else if (provider.equals(LocationManager.NETWORK_PROVIDER)){ tvNET.setText("Status: " +String.valueOf(status)); } } }; private String formatLocation(Location location){ if (location==null)return ""; CoordLat=location.getLatitude(); CoordLen=location.getLongitude(); return String.format("Coordinates: lat = %1$.4f, lon = %2$.4f, time = %3$tF %3$tT", location.getLatitude(), location.getLongitude(), new Date(location.getTime())); } private void showLocation (Location location) { if (location == null) return; if (location.getProvider().equals(locationManager.GPS_PROVIDER)) { tvGPS.setText(formatLocation(location)); } else if (location.getProvider().equals(LocationManager.NETWORK_PROVIDER)) { tvNET.setText(formatLocation(location)); } } final static int PERMISSION_ALL =1; final static String[] PERMISSIONS ={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}; private void onResume (Bundle savedInstanceState){ locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener); } private void onPause (Bundle savedInstanceState){ locationManager.removeUpdates(locationListener); } } 

activity_maps.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="gavno.ssod22.MapsActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" android:id="@+id/tvGPS" android:layout_marginTop="34dp" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:layout_marginStart="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" android:id="@+id/tvNET" android:layout_marginTop="34dp" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:layout_marginStart="250dp" /> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="400dp" tools:context="gavno.ssod22.MapsActivity" android:layout_below="@+id/tvGPS" android:layout_alignParentStart="true" android:layout_marginTop="25dp" android:layout_alignParentBottom="true" /> </RelativeLayout> 

    1 answer 1

    It would be great if the MapsActivity class would contain only what is necessary for working with maps. And it’s not quite clear how geo-position is passed to variables through mainActivity? I once worked with maps, but realized everything by transferring data from mainActivity to the mapsActivity class variables:

     Intent intent = new Intent(Main.this, GMap.class); intent.putExtra("Latitude", Double.parseDouble(latitude)); intent.putExtra("Longitude", Double.parseDouble(longitude)); 

    Hope to help.

    • In text variables, the longitude and latitude goes to the checkEnabled () function in activity_maps - Pavel Gudkov