user1163234
user1163234

Reputation: 2497

Cant see the Marker or Zoom in GoogleMap in SupportMapFragment

I have a SupportMapFragment that I want to add a simple marker to but for some reason I cant see it on the map...I dont see any error in the logcat either any idea?

public class MapUserFragment extends SupportMapFragment
{

    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.map_fragment_layout, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {

        super.onActivityCreated(savedInstanceState);
        googleMap = getMap();
        if (googleMap != null)
        {
            try
            {

                googleMap.addMarker(new MarkerOptions().position(new LatLng(27.174840, 78.042500)).title("Hello World")
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_drawer)));

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:uiCompass="true"
map:mapType= "normal"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true" 
/>

google-play-services_lib manifest

<?xml version="1.0" encoding="utf-8"?>

package="com.google.android.gms"
android:versionCode="3159130"
android:versionName="3.1.59 (744626-30)" >

<uses-sdk android:minSdkVersion="8"/>

MyManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tailgate"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.tailgate.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<!--
 Creates a custom permission so only this app can receive its messages.

 NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
       where PACKAGE is the application's package name.
-->
<permission
    android:name="com.tailgate.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />


<!-- Location permission -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature
    android:name="android.hardware.location"
    android:required="true" />
<uses-feature
    android:name="android.hardware.location.gps"
    android:required="false" />

<application
    android:name=".TailGateApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <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>


    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCKHkansQagp34Q5hiEEDm5XMPhT_Izntc" />
</application>

Edit:

I also cant zoom into a certain LatLong

    LatLng cur_Latlng =new LatLng(31.763160705566406, 35.20343017578125);
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(cur_Latlng));
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(10)); 
            googleMap.getUiSettings().setZoomControlsEnabled(true);

Upvotes: 2

Views: 756

Answers (2)

Serafins
Serafins

Reputation: 1247

Exactly, now You are calling initMap() <- method which is invoke in onCreateView. Before you tried to create marker in SupportMapFragment. This method is called before map is created, even before onCreate Here you can read more about it

Upvotes: 1

user1163234
user1163234

Reputation: 2497

Found the soltution below:

However I dont know why this is? Is it because I need to add the markers in OnCreateView?

 public class MapUserFragment extends SupportMapFragment
 {
      public MapUserFragment() {
        super();
    }

    @Override
    public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
        View v = super.onCreateView(arg0, arg1, arg2);
        initMap();
        return v;
    }

    private void initMap(){
        LatLng lat = new LatLng(0, 0);
        UiSettings settings = getMap().getUiSettings();
        settings.setAllGesturesEnabled(false);
        settings.setMyLocationButtonEnabled(false);

       // getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(lat,16));
        getMap().addMarker(new MarkerOptions().position(lat));
    }

Upvotes: 2

Related Questions