Can't Manipulate GoogleMap by supportMapFragment

315 Views Asked by At

EDIT: To create a marker and add it to the map. This method is called after the map is called.

public void testDisplay(){
    // latitude and longitude
    double latitude = 0;
    double longitude = 0;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));

    // adding marker
    map.addMarker(marker);
}

I am developing a Java android Application that provides an itinerary. I implemented google maps. But I can't seem to manipulate the map (Add markers, move around, add location user etc.)

Here are my imports (Maybe I am importing a different library of the same type):

import android.content.Context;


import android.os.Bundle;

import android.util.Log;

import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.codexmalta.mytravelbuddy.R;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;  

import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

Here is my methods that contains the lines to get the map and I am calling it from the onCreate():

SupportMapFragment fm = new SupportMapFragment();

public void getMapFragment(){
        fm = (SupportMapFragment)   getSupportFragmentManager().findFragmentById(R.id.map);
        map = fm.getMap();
        Log.e("getmapfragment0", "got map fragment");
        map.setMyLocationEnabled(true);
        map.setMapType(map.MAP_TYPE_SATELLITE);
    }

I also checked for NULL on map and it shows me that map is not NULL.

The map does not even change to Satellite. The button to get current location does not show up. What can I do to fix this? I am lost and I need to present this application on January.

1

There are 1 best solutions below

2
On BEST ANSWER

try this

Activity must be FragmentActivity

SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap(); 

For adding marker

Marker mapPinPoint = mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

calltestDisplay() after initialize the map from onCreate