1

We have the Google Maps API working; however, current location will not show up in our app but will show up when redirected to Google Maps(app). We want the blue dot to show up in our app and not just when it is forwarded to Google Maps(app).

Here are some code snippets (we are using Android Studio V2.3.3):

Can anyone offer any guidance, please?

Here is our MapActivity.java

package com.example.administrator.bubbleproject;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
    private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        LatLng losAngeles = new LatLng(34.0522, -118.2437);
        mMap.addMarker(new MarkerOptions().position(losAngeles).title("Los Angeles"));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(losAngeles, 18.0f));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mMap.setMyLocationEnabled(true);
    }
}

Here is our Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.bubbleproject">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="BUBBLE"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:parentActivityName="com.example.administrator.bubbleproject.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".tracker1PageActivity"
            android:parentActivityName=".MainActivity" />

        <activity android:name=".settingsActivity" />
        <activity android:name=".setupActivity" />

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
        <activity
            android:name=".MapActivity"
            android:label="@string/title_activity_map">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Here are some screenshots of My app and Forwarded to google maps.

K.Os
  • 5,123
  • 8
  • 40
  • 95
Jojohobo
  • 11
  • 1
  • 3

1 Answers1

0

My guess is that in OnMapReady() your permission check is failing and it's executing the return; instead of calling mMap.setMyLocationEnabled(true); Set a breakpoint and check it.

My code to check this looks like the following:

public static boolean haveLocationPermission(Context context)
{
    return ContextCompat.checkSelfPermission(context, permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
               ContextCompat.checkSelfPermission(context, permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}

Then call this snippet when you want to check permission:

// Enable my location if we have permission
if (haveLocationPermission(getActivity()))
{
    try
    {
        mMap.setMyLocationEnabled(true);
    }
    catch (SecurityException e) {}
}

We also have Location Services turned on, but I'm not sure whether that is necessary for the map to show location (use GoogleApiClient with the LocationServices.API).

Edit: From the duplicate issue it looks like you do need Location Services turned on. https://stackoverflow.com/a/34582595/1022886

jt-gilkeson
  • 2,661
  • 1
  • 30
  • 40
  • getActivity() seems to be popping up as an error... – Jojohobo Jul 28 '17 at 17:56
  • You should be able to substitute MapActivity.this since you are in an activity (my example is from a fragment) – jt-gilkeson Jul 28 '17 at 17:59
  • I seem to be getting a lot of errors for if (haveLocationPermission(getActivity())) – Jojohobo Jul 28 '17 at 18:19
  • 1) "illegal start of type" right before the 'if' satement 2) " expected" right after 'haveLocationPermission 3) " ';' expected" right before 'MapActivity.this' 4) "illegal start of type" right before the dot in '.this' 5) "as of release 8, 'this' is allowed as the parameter name for the receiver type only, which has to be the first parameter" right before the 't' in '.this' – Jojohobo Jul 28 '17 at 18:26
  • By the way, I inputted 'if(haveLocationPermission(MapActivity.this))' not 'if(haveLocationPermission(getActivity()))'. Sorry for the miscommunication. – Jojohobo Jul 28 '17 at 18:31
  • Did you put that haveLocationPermission code inside your onMapReady()? Did you try setting a breakpoint on your original code? You should check the solution to the question that has been marked as the duplicate - it looks like from that answer, that you do need LocationService running. – jt-gilkeson Jul 28 '17 at 18:34
  • What did you define 'haveLocationPermission' as within the 'onMapReady()' code? – Jojohobo Jul 28 '17 at 18:50