1

I'm trying to make an app with a button called "Map" that will display a city map once pressed. So far I've made an app which contains the Map button and a separate app for my map which uses the Google Developers' tutorial to display a map when the app is opened. I'm having trouble when I try to combine both apps. I'm working on the OnClickListener but I don't know what I should put so that the map would be displayed after pressing the button.

EDIT

Below is my MapFragment class for the map:

    public class MapFragment extends FragmentActivity
    {
        private GoogleMap mMap;
        private LatLngBounds ILOILO = new LatLngBounds(new LatLng(10.6, 122.466), new LatLng(10.716, 122.566));

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View v = inflater.inflate(R.layout.map_layout, container, false);

            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            mMap.setMyLocationEnabled(true);
            mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ILOILO.getCenter(), 10));

            if (mMap == null)
            {
                 Toast.makeText(this, "Google Maps not available", 
                     Toast.LENGTH_LONG).show();
            }

            return v;
        }
    }

Here's my XML file for the map:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    map:uiCompass="false"
    map:uiTiltGestures="true"
    map:uiZoomControls="true"
    map:uiZoomGestures="true"
    map:uiRotateGestures="true"
    map:uiScrollGestures="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>

Here's for the button located in my MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageButton mapButton = (ImageButton) findViewById(R.id.mapButton);
    mapButton.setOnClickListener(mapListener);
}

private OnClickListener mapListener = new OnClickListener()
{   
    public void onClick(View v)
    {   
        Intent i = new Intent(MainActivity, MapFragment.class);
        startActivity(i);
    }
};

I've also modified my Manifest since I've been getting "Unable to find explicit activity class (com.example.ipp.MapFragment); have you declared this activity in your AndroidManifest.xml?" error. Below is my manifest.

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="MapFragment" >
        </activity>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

PROBLEM

When I try to click the "Map" button, it displays my UI instead of displaying the map I've prepared.

Ann
  • 32
  • 1
  • 2
  • 7
  • I dont' see a `R.id.map` anywhere in your code... – shkschneider Dec 04 '14 at 16:50
  • @shkschneider Here's my xml file for the map Here's for the button: ImageButton mapButton = (ImageButton) findViewById(R.id.mapButton); mapButton.setOnClickListener(mapListener); I don't know what I should do for the OnClickListener. I don't know if that's what you mean with the R.id.map – Ann Dec 04 '14 at 18:44
  • So you have 2 apps. First app is working correctly and you can see your map. Now you want access from othe app on button click first app and show map here. Did I understand you correctly? – Anatol Dec 04 '14 at 19:23
  • @Anatol Yes. I've tried using MapView like most of the tutorials I've found on the web but it opens the Google Maps app on my device when I press the map button instead of displaying the map that I've prepared. – Ann Dec 05 '14 at 01:21
  • @Sher Ann Vicsie Acasio, i don't now what tutorials are you trying to accomplish. First you should check official documentation. https://developers.google.com/maps/documentation/android/start Next if you want to use google maps inside fragment you should check this. http://stackoverflow.com/questions/19353255/how-to-put-google-maps-v2-on-a-fragment-using-viewpager Second Answer with 15 points helps me to achieve this. Good luck. – Anatol Dec 05 '14 at 08:59
  • Yeah, just re-follow a google tutorial. Also, try to use `com.google.android.gms.maps.SupportMapFragment` as `MapFragment` – shkschneider Dec 05 '14 at 09:16
  • Thank you both for the advise guys :) – Ann Dec 09 '14 at 07:00
  • Found the problem. I put `setContentView(R.layout.activity_main);` in the MapFragment.java instead of `setContentView(R.layout.map_layout);` – Ann Dec 10 '14 at 15:59

0 Answers0