【问题标题】:access location with google maps使用谷歌地图访问位置
【发布时间】:2017-07-11 22:18:44
【问题描述】:

我想使用谷歌地图访问我的位置,但我不知道为什么它不起作用。

公共类 MapsActivity 扩展 FragmentActivity 实现 OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // 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;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

这就是我统治模拟器时出现的内容:

现在我尝试学习有关地图的某事,但最后我想按下一个按钮并在谷歌地图中显示餐厅的位置或其他某事。我该怎么做?

【问题讨论】:

    标签: android


    【解决方案1】:

    这是访问设备位置的完整代码(不包括我认为您已经完成的地图关键部分)

    public class MapsActivity extends FragmentActivity implements
        OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener
         {
    
    private GoogleMap mMap;
    private GoogleApiClient mGoogleApiClient;
    public Location mLocation;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
    
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
    
        mMap = googleMap;
    
    
    }
    
    @Override
    protected void onStart() {
        mGoogleApiClient.connect();
    
        super.onStart();
    }
    
    @Override
    protected void onStop() {
        mGoogleApiClient.disconnect();
    
        super.onStop();
    
    }
    
    
    public void onConnected( Bundle bundle) {
    
    
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
        }
        if (!mGoogleApiClient.isConnected()) {
            mGoogleApiClient.connect();
        }
        Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLocation != null && mMap != null) {
            LatLng tun = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
    
            mMap.addMarker(new MarkerOptions().position(tun).title("Marker in Sydney"));
    
    
        }
    
    
    }
    
             @Override
             public void onConnectionSuspended(int i) {
    
             }
    
             @Override
             public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    
             }
         }
    

    您也必须在 manifest.xml 中编写此代码

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

    这将允许应用程序能够使用设备的位置

    剩下的唯一事情是您必须通过进入权限菜单中的应用程序设置来授予对位置的访问权限允许应用程序获取 GPS 位置,否则它不会显示位置

    【讨论】:

    • 我无法解析“LocationServices”,你知道为什么吗?
    • 老兄,对不起,我是堆栈和 android 工作室的新手。我刚刚将您的代码复制到我的代码上,并对 manifest.xml 进行了更改。我的代码包含在您之前发送的内容中
    • 你是否像我在 MainActivity.java DUDE 中那样实现了所有功能?
    • 导入android.location.LocationListener;
    • 我在哪里可以找到应用程序的设置?顺便说一句,还是不行。
    【解决方案2】:

    'com.google.android.gms:play-services:11.0.2' 您必须编译播放服务才能访问 LocationListener

    【讨论】:

    • 这是一个新的导入
    • 它不是 java 文件中的导入,您必须在依赖项的部分中将这段代码写入您的应用程序级别
    • 我已经有了这个 'com.google.android.gms:play-services-maps:11.0.2'
    • 把你的 gradle:app 截图发给我
    【解决方案3】:

    您是否在清单中添加了访问位置权限

    【讨论】:

    • 是的,其中一个答案包含权限,我复制到那里
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多