【问题标题】:how to use setMyLocationEnabled(true) and addMarker in google maps v2?如何在谷歌地图 v2 中使用 setMyLocationEnabled(true) 和 addMarker?
【发布时间】:2018-02-08 13:59:45
【问题描述】:

我尝试使用setMyLocationEnabled(true),但需要权限,我已经添加了它,但它仍然无法正常工作。也是标记。我尝试在 MainActivity.java 中编写代码,但它仍然无法正常工作。

这是我的 MapsActivity.java

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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private LatLng latLngBiru = new LatLng(35.6829733, 139.7321275);
    private LatLng latLngKuning = new LatLng(35.6847009, 139.7314891);
    private LatLng latLngMerah = new LatLng(35.6839537, 139.7308615);

    @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);

        mMap.addMarker(new MarkerOptions()
                .position(latLngBiru)
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                .title("tempat rahasia").snippet("rahasia lho"));

        mMap.addMarker(new MarkerOptions()
                .position(latLngKuning)
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
                .title("bangunan kampus").snippet("bangunan utama"));

        mMap.addMarker(new MarkerOptions()
                .position(latLngMerah)
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED))
                .title("kantin kampus").snippet("makan makan"));

    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        MarkerOptions marker = new MarkerOptions().position(new LatLng(-6.21462, 106.84513)).title("Hello Maps ");
        mMap.addMarker(marker);
        CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(-6.21462, 106.84513)).zoom(12).build();

        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        }


    }
}

这是我的 activity_maps.xml

<LinearLayout 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">

   <fragment
       android:id="@+id/map"
       android:name="com.google.android.gms.maps.SupportMapFragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context="com.mqa.android.ewasapp.MapsActivity" />

</LinearLayout>

这是我的 activity_main.xml

这里有谷歌地图显示的地方

<FrameLayout 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">

<!-- TODO: Update blank fragment layout -->
    <FrameLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.mqa.android.ewasapp.MapsActivity" />

</FrameLayout>

请至少帮助我设置MyLocationEnable。之前谢谢你。

【问题讨论】:

    标签: java android xml google-maps


    【解决方案1】:

    使用位置请求权限

    private boolean mayRequestLocation(){
    
        ActivityCompat.requestPermissions(LoginActivity.this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                1);
    
        ActivityCompat.requestPermissions(LoginActivity.this,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                1);
    
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return true;
        }
    
            return false;
    }
    

    【讨论】:

    • 我应该把 setMyLocationEnable(true) 放在哪里?
    • @QubaisyAndyiantama setMyLocationEnable 只是在地图上出现一个按钮来关注位置
    • @QubaisyAndyiantama 错误是因为许可吗?
    • 它没有任何错误,但无法在右上角显示 MyLocation 按钮
    • @QubaisyAndyiantama 但应用程序有权访问该位置?
    猜你喜欢
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2014-08-01
    • 1970-01-01
    相关资源
    最近更新 更多