1

Does Google allow using MapView versus MapFragments or SupportMapFragments on v2? I have a layer.xml file that is using the SupportMapFragment how do I change it to a MapView while utilizing the LinearLayout and RelativeLayouts I am using below?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/btn_draw"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=" Get Directions"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_find"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_btn_find"
            android:layout_alignParentRight="true" />

        <EditText
            android:id="@+id/et_location"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="@string/hnt_et_location"
            android:layout_toLeftOf="@id/btn_find" />

    </RelativeLayout>



    <fragment 
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>

How does it change my code as well?

/**
 * Shows the users current location on the map
 */
private void showCurrentLocationOnMap(){
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mf= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    map = mf.getMap();

    //map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    ll = new PointLocationListener();

    boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!isGPS){
        Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", true);
        sendBroadcast(intent);
    }

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, ll);
}
yams
  • 942
  • 6
  • 27
  • 60

1 Answers1

3

Does Google allow using MapView versus MapFragments or SupportMapFragments on v2?

Yes.

how do I change it to a MapView while utilizing the LinearLayout and RelativeLayouts I am using below?

AFAIK, it should simply be:

Step #1: Replace your <fragment> with:

<com.google.android.gms.maps.MapView 
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

Step #2: Follow the instructions on the MapView class documentation to forward lifecycle events to the view

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So this app has been published does this change anything with the API Key? I previously used the SHA1 to generate the key? – yams Jun 28 '13 at 00:33
  • @MarkBasler: This should have no impact on your API key. – CommonsWare Jun 28 '13 at 00:33
  • Now what about my activity that extends the mapactivity will that change the code quite a bit? – yams Jun 28 '13 at 00:34
  • For example lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mf= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); map = mf.getMap(); – yams Jun 28 '13 at 00:37
  • @MarkBasler: There is no `MapActivity`. That was a Maps V1 thing. You are using Maps V2. There is no `MapActivity` in Maps V2. You use a regular `Activity`. – CommonsWare Jun 28 '13 at 00:41
  • Oh yeah it extends FragmentActivity but How does that affect the Java code I posted above in my original question? – yams Jun 28 '13 at 00:43
  • @MarkBasler: To find a widget in a layout, you call `findViewById()`, as you probably have done countless times. To get a `GoogleMap` from a `MapView`, you call `getMap()` on the `MapView`, as you can tell by reading the documentation for `MapView`, as I linked to earlier. – CommonsWare Jun 28 '13 at 00:44
  • You can't get the map from the mapView. I Guess I'll leave as is and close this question. Can you take a look at my question on http://stackoverflow.com/questions/17354324/how-do-i-clear-the-map-of-all-points-and-markers-and-routes-when-switching-tabs/17354456?noredirect=1#comment25183968_17354456 – yams Jun 28 '13 at 00:49
  • @MarkBasler: "You can't get the map from the mapView" -- call `getMap()` on `MapView` to get the `GoogleMap`, as I wrote previously, and is documented in the JavaDocs for `MapView`: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapView.html#getMap() – CommonsWare Jun 28 '13 at 00:50
  • @MarkBasler: Make sure that you are importing the *Maps V2* `MapView`, not the Maps V1 `MapView`. In fact, I would recommend that if your build target is a Google APIs edition, switch back to a standard one, so you do not get confused and use the wrong imports. – CommonsWare Jun 28 '13 at 01:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32531/discussion-between-mark-basler-and-commonsware) – yams Jun 28 '13 at 01:28
  • What are the motivations to use MapFragment instead of SupportMapFragment? – cassioso Nov 20 '13 at 18:37
  • 1
    @cassioso: `MapFragment` is for use with the native API Level 11 implementation of fragments. `SupportMapFragment` is for use with the Android Support package's backport of fragments. – CommonsWare Nov 20 '13 at 18:52
  • How on earth do you forward calls to onCreate to your view when it hasn't been created yet? Or is that for activities only? – ZaBlanc Jun 25 '14 at 21:49
  • @ZaBlanc: I have not personally used `MapView` directly. I would presume that if it is hosted by an `Activity`, you call `onCreate()` on the `MapView` in `onCreate()` of the `Activity`, when you create the `MapView`. If it is hosted by a `Fragment`, call `onCreate()` on the `MapView` from `onCreateView()` of the `Fragment`, when you create the `MapView`. But, that's just a guess. – CommonsWare Jun 25 '14 at 21:55