谷歌地图api没有显示我当前的位置

时间:2016-09-30 20:04:22

标签: java android api google-maps

我关注了一些文档,但它仍然没有显示我当前的位置,这是我的代码:

package com.example.stewiesh.uniguide;

import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

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 Routes extends FragmentActivity implements OnMapReadyCallback
{
    private GoogleMap mMap;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_routes);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    public void onMapReady(GoogleMap googleMap)
    {
        mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            System.out.println("Permission error");
            return;
        }
        mMap.setMyLocationEnabled(true);
        mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener()
        {
            public boolean onMyLocationButtonClick()
            {
                System.out.println("button error");
                return false;
            }
        });
    }

}

我正在使用api的最新版本,清单和gradle中的所有内容都很好,我已经多次检查过了....

2 个答案:

答案 0 :(得分:0)

替换onMyLocationButtonClick中的代码:

public boolean onMyLocationButtonClick()
    {
        mMap.getMyLocation();
    }

答案 1 :(得分:0)

我检查过,你的代码工作正常。

检查,也许在模拟器中(例如,如果你使用Genymotion)你没有打开GPS,这就是为什么它没有显示你的位置......

这是工作代码:

@Override
public void onMapReady(GoogleMap googleMap) {
    Log.d(TAG, "onMapReady()");
    mGoogleMap = googleMap;
    /*
    * Adding map control buttons
    * */
    mUiSettings = mGoogleMap.getUiSettings();
    mUiSettings.setZoomControlsEnabled(true);

    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        Log.d(TAG, "PERMISSION_GRANTED");

        /*
        * Allow application to get user location
        * */
        mGoogleMap.setMyLocationEnabled(true);



    } else {
        Log.d(TAG, "PERMISSION_NOT_GRANTED");
        Toast.makeText(getActivity(), "Permissions to get the location are not granted. Please setup the permissions.", Toast.LENGTH_LONG).show();
    }
}