【问题标题】:Blue dot and circle is not shown on MyLocation using android fused location api使用 android fused location api 在 MyLocation 上未显示蓝点和圆圈
【发布时间】:2015-07-27 00:02:15
【问题描述】:

我使用 LocationManager 来跟踪用户当前位置,现在将位置管理器更改为 FusedLocation API 后,即使设置map.setMyLocationEnabled(true),蓝点和圆圈也不会显示。我可以在我的地图片段的右上角看到当前位置图标,但单击它什么也没做。我将我的代码恢复到 LocationManager 现在我可以看到指向我当前位置的蓝点。使用 Fused Location API 会出现什么问题。

【问题讨论】:

  • 关于MyLocation 按钮,您使用什么并不重要。您根本不需要为该按钮添加任何代码,蓝色标记即可工作。
  • @DanielNugent,但是点击右上角的 MyLocation 按钮不起作用,我在当前位置上看不到蓝色圆圈指针。
  • 这很奇怪。你能展示一些你的代码吗?你在使用SupportMapFragment吗?
  • 是的,我正在使用SupportMapFragment
  • 我刚刚在 Android Studio 中新建了一个空白项目,唯一需要添加的代码是:mFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); map = mFragment.getMap(); map.setMyLocationEnabled(true);

标签: android google-maps-android-api-2 android-fusedlocation


【解决方案1】:

针对 api-23 或更高版本

this answer....

针对 api-22 及更低版本:

此代码适用于我,它有 MyLocation 蓝点/圆圈,它还使用 Fused Location Provider 在当前位置放置 Marker

这是我使用的整个 Activity 代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.location.Location;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.OnMapReadyCallback;

public class MainActivity extends AppCompatActivity implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
        LocationListener,
        OnMapReadyCallback {

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;

    LatLng latLng;
    GoogleMap mGoogleMap;
    SupportMapFragment mFragment;
    Marker mCurrLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        mGoogleMap = googleMap;

        mGoogleMap.setMyLocationEnabled(true);

        buildGoogleApiClient();

        mGoogleApiClient.connect();
    }

    @Override
    public void onPause() {
        super.onPause();
        //Unregister for location callbacks:
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }

    protected synchronized void buildGoogleApiClient() {
        Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle bundle) {
        Toast.makeText(this,"onConnected",Toast.LENGTH_SHORT).show();
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            //place marker at current position
            mGoogleMap.clear();
            latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("Current Position");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            mCurrLocation = mGoogleMap.addMarker(markerOptions);
        }

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000); //5 seconds
        mLocationRequest.setFastestInterval(3000); //3 seconds
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLocationChanged(Location location) {

        //remove previous current location marker and add new one at current position
        if (mCurrLocation != null) {
            mCurrLocation.remove();
        }
        latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mCurrLocation = mGoogleMap.addMarker(markerOptions);

        Toast.makeText(this,"Location Changed",Toast.LENGTH_SHORT).show();

        //If you only need one location, unregister the listener
        //LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }

}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

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

</RelativeLayout>

结果:

【讨论】:

  • 这对我有用。但是,我需要使用存储在 LatLang 对象中的位置。当我尝试访问它们时,我得到一个空指针异常。有什么想法吗?
  • @bholagabbar 如果需要,您可以存储对在onLocationChanged() 中返回的当前位置对象的引用,此代码使用 LatLng。每次使用时请务必检查它是否为空,因为位置可能尚未进入。任何依赖于位置锁定的东西,如果需要,在 onLocationChanged() 中启动它。
  • 我尝试制作 latLang gloabl,但我仍然得到 nullpointerException。我不明白你的方式。你能详细说明一下吗?
  • @bholagabbar 在调用第一个位置回调onLocationChanged() 之前,LatLng 对象将为 null,之后为非 null。如果您尝试在onCreate() 中访问latLng,它将无法正常工作,因为它还没有进入onLocationChanged()
  • @MuhammadShahzad 您是否在 Android M (Android 6) 上进行测试?如果是这样,您将需要在运行时提示用户,请参见此处:stackoverflow.com/questions/33063712/…
【解决方案2】:

您必须在清单中添加以下权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

【讨论】:

  • 哇...谢谢@MuhammadMoosa!我花了很多时间试图弄清楚为什么我的位置在一个应用程序中不准确,而当我调试我的另一个应用程序时它是完美的......我检查了很多次权限,我唯一缺少的是&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;。我什至不认为这个权限与地图有关..
  • 其实你只需要一个。 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION,取决于您的实现。
【解决方案3】:
MarkerOptions().position(new LatLng(
                            location.getLatitude(), location.getLongitude()));

试试这个,

   if (location!=null) {
                googleMap.clear();
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(14).build();
                googleMap.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));

                // create markerOptions
                MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(
                        location.getLatitude(), location.getLongitude()));
                // ROSE color icon
                markerOptions.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
                markerOptions.position(latLng);
                // adding markerOptions
                Marker marker = googleMap.addMarker(markerOptions);
                dropPinEffect(marker);
            }

【讨论】:

    猜你喜欢
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2017-05-04
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多