polkaspot01
polkaspot01

Reputation: 33

Why is setMyLocationEnabled(true) and other activities on map are not functioning?

I faced problem on my google map which is in a fragment. Even I have the code with

map.setMyLocationEnabled(true);                    
map.getUiSettings().setAllGesturesEnabled(true); 
LatLng pos= new LatLng(1.464,110.426); 
map.addMarker(new MarkerOptions().position(pos));

my map has never shown me anything as in the code. Blue indicator is not shown as my current location and marker cannot be seen.

It works with a pedometer where the pedometer details(steps, distance, speed, calories) are showing on top while the map is showing below the pedometer details. Below are my codes:

fragment_pedo.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/screen_background"
    android:orientation="vertical"
    android:padding="@dimen/margin" >

    //more codes, XML for pedo

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

</LinearLayout>

MapFragment.java

public class MapFragment extends Fragment implements
        LocationListener, GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener {

    private static View v;
    private LocationRequest lr;
    private LocationClient lc;
    private LatLng point;
    GoogleMap map;

    float mLat, mLng;
    Marker marker;

    public MapFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.d("location", "onCreateView");


            v = inflater.inflate(R.layout.fragment_pedo, container, false);
            SupportMapFragment mapFragment = ((SupportMapFragment) getActivity()
                    .getSupportFragmentManager().findFragmentById(R.id.map));

            map = mapFragment.getMap();

            map.getUiSettings().setAllGesturesEnabled(true);
            map.getUiSettings().setRotateGesturesEnabled(true);
            map.getUiSettings().setMyLocationButtonEnabled(true);
            map.setMyLocationEnabled(true);
            map.getUiSettings().setZoomControlsEnabled(true);
            LatLng pos= new LatLng(1.464,110.426); 
            map.addMarker(new MarkerOptions().position(pos));

            MapsInitializer.initialize(this.getActivity());
            return v;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Log.d("location", "onCreate");
        lr = LocationRequest.create();
        // lr.setInterval(1000);
        lr.setNumUpdates(2);
        lc = new LocationClient(this.getActivity().getApplicationContext(),
                this, this);
        lc.connect();

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public void onConnected(Bundle connectionHint) {
        // TODO Auto-generated method stub
        Log.d("location", "onConnected");
        Toast.makeText(getActivity(), "You may long tap to set your location.", Toast.LENGTH_LONG).show();
        lc.requestLocationUpdates(lr, this);

    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub
        Log.d("location", "onDisconnected");
        Toast.makeText(getActivity(), "Disconnected. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLocationChanged(Location arg0) {

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(arg0.getLatitude(), arg0.getLongitude()))
                .zoom(15).build();
        Log.d("location", "myLatLng=" + cameraPosition);

        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        point = new LatLng(arg0.getLatitude(), arg0.getLongitude());
        mLat = (float) point.latitude;
        Log.d("tag", "mLat=" + mLat);
        mLng = (float) point.longitude;
        Log.d("location", "mLng=" + mLng);
        myMarker();
        Log.d("location", "do you run?" + "do you run?");

    }

    // add a new marker
    public Marker myMarker() {
        marker = map.addMarker(new MarkerOptions().position(point).title(
                point.toString()));
        return marker;

    }

}

Why is my current location and other activity on map cannot be shown? Please help. Million thanks in advance.

Upvotes: 0

Views: 6517

Answers (1)

uiltonsantos
uiltonsantos

Reputation: 409

Check if you added correctly the permissions in your AndroidManifest.

// Required to show current location

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

//The Internet permission is required too.

<uses-permission android:name="android.permission.INTERNET" />

To show your Marker, remember of the zoom and animate the camera, like this:

LatLng latLng = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions()
                       .position(latLng)
                       .title("title")
                       .snippet("some text in ballon");
CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(18).build();//18 was a number it was fine to my situation.

Marker markerFinal = googleMap.addMarker(markerOptions);
markerFinal.showInfoWindow();//the marker comes with balloon already open.

googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

Regards,

Uilton Santos.

Upvotes: 3

Related Questions