Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- package com.joythis.android.gmaps22_2;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.fragment.app.Fragment;
- import androidx.fragment.app.FragmentManager;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.Spinner;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.SupportMapFragment;
- import java.util.Calendar;
- public class MainActivity extends AppCompatActivity {
- public final static int CALLBACK_CODE_FOR_WHEN_PERMISSIONS_ARE_READY = 123;
- SupportMapFragment mMapFragment;
- FragmentManager mSupportFragmentManager;
- AmGeoMap mAmGoogleMap;
- AmUtil mUtil;
- Spinner mSpnMapType;
- AdapterView.OnItemSelectedListener mItemSelectedHandler = new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- String strSelectedOption = parent.getItemAtPosition(position).toString();
- boolean bHybrid = strSelectedOption.compareToIgnoreCase(getString(R.string.strMapTypeHybrid))==0;
- boolean bNone = strSelectedOption.compareToIgnoreCase(getString(R.string.strMapTypeNone))==0;
- boolean bNormal = strSelectedOption.compareToIgnoreCase(getString(R.string.strMapTypeNormal))==0;
- boolean bSat = strSelectedOption.compareToIgnoreCase(getString(R.string.strMapTypeSatellite))==0;
- boolean bTerrain = strSelectedOption.compareToIgnoreCase(getString(R.string.strMapTypeTerrain))==0;
- int iGoogleMapType = 0;
- if (bHybrid) iGoogleMapType = GoogleMap.MAP_TYPE_HYBRID;
- if (bNone) iGoogleMapType = GoogleMap.MAP_TYPE_NONE;
- if (bNormal) iGoogleMapType = GoogleMap.MAP_TYPE_NORMAL;
- if (bSat) iGoogleMapType = GoogleMap.MAP_TYPE_SATELLITE;
- if (bTerrain) iGoogleMapType = GoogleMap.MAP_TYPE_TERRAIN;
- if (mAmGoogleMap !=null) mAmGoogleMap.changeMapType(iGoogleMapType);
- }//mItemSelectedHandler
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- //do nothing
- }
- };//mItemSelectedHandler
- final float
- DEF_LATITUDE=39.22086f, //ESGTS in decimal degrees
- DEF_LONGITUDE=-8.686735f, //ESGTS in decimal degrees
- DEF_ZOOM=18.0f; //higher value, closer shot
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.rl_map);
- init();
- }//onCreate
- void init(){
- mUtil = new AmUtil(this);
- mSupportFragmentManager = this.getSupportFragmentManager();
- mSpnMapType = findViewById(R.id.idSpnMapType);
- mSpnMapType.setOnItemSelectedListener(mItemSelectedHandler);
- GeoUri someInitialLocation = new GeoUri(
- DEF_LATITUDE,
- DEF_LONGITUDE,
- DEF_ZOOM,
- "ESGTS",
- Calendar.getInstance()
- );
- //AmGeoMap implements OnMapReadyCallback, so its objects can be arguments of getMapAsync
- mAmGoogleMap = new AmGeoMap(
- this,
- CALLBACK_CODE_FOR_WHEN_PERMISSIONS_ARE_READY,
- someInitialLocation
- );
- Fragment f = mSupportFragmentManager.findFragmentById(R.id.idMap);
- mMapFragment = (SupportMapFragment) f;
- mMapFragment.getMapAsync(mAmGoogleMap);
- }//init
- }//MainActivity
- ****
- my_arrays.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="saMapTypes">
- <item>@string/strMapTypeHybrid</item>
- <item>@string/strMapTypeNone</item>
- <item>@string/strMapTypeNormal</item>
- <item>@string/strMapTypeSatellite</item>
- <item>@string/strMapTypeTerrain</item>
- </string-array>
- <string name="strMapTypeTerrain">Terrain</string>
- <string name="strMapTypeSatellite">Satellite</string>
- <string name="strMapTypeNormal">Normal</string>
- <string name="strMapTypeNone">None</string>
- <string name="strMapTypeHybrid">Hybrid</string>
- </resources>
- ******
- fragment_to_be_included.xml
- <?xml version="1.0" encoding="utf-8"?>
- <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/idMap"
- android:name="com.google.android.gms.maps.SupportMapFragment"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" />
- <!--
- <fragment android:id="@+id/idMap"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- class="com.google.android.gms.maps.MapFragment"
- xmlns:android="http://schemas.android.com/apk/res/android" />
- -->
- ********
- rl_map.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <TextView
- android:id="@+id/idTvDashboard"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <Spinner
- android:layout_below="@id/idTvDashboard"
- android:id="@+id/idSpnMapType"
- android:entries="@array/saMapTypes"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <include
- android:layout_below="@id/idSpnMapType"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- layout="@layout/fragment_to_be_included"/>
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement