android - Current Location(on Google Maps API) not showing in app -
this question has answer here:
we have google maps api working; however, current location not show in our app show when redirected google maps(app). want blue dot show in our app , not when forwarded google maps(app).
here code snippets (we using android studio v2.3.3):
can offer guidance, please?
here 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 supportmapfragment , notified when map ready 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 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 screenshots of my app , forwarded google maps.
my guess in onmapready()
permission check failing , it's executing return;
instead of calling mmap.setmylocationenabled(true);
set breakpoint , check it.
my code check looks 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 snippet when want check permission:
// enable location if have permission if (havelocationpermission(getactivity())) { try { mmap.setmylocationenabled(true); } catch (securityexception e) {} }
we have location services turned on, i'm not sure whether necessary map show location (use googleapiclient locationservices.api).
edit: duplicate issue looks need location services turned on. https://stackoverflow.com/a/34582595/1022886
Comments
Post a Comment