【问题标题】:Current Location button is not showing当前位置按钮未显示
【发布时间】:2017-01-15 01:57:06
【问题描述】:

我在 android 应用程序中通过 tab(viewPager) 使用谷歌地图 V2,我想在地图中显示当前位置,但我找不到这个按钮。

AddMasjid.java

package com.example.saroosh.masjidnow.Tabs;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.saroosh.masjidnow.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class AddMasjid extends Fragment implements OnMapReadyCallback{

    MapView gMapView;
    GoogleMap gMap = null;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.add_masjid,container,false);

        gMapView = (MapView) v.findViewById(R.id.map);
        gMapView.getMapAsync(this);
        gMapView.onCreate(savedInstanceState);
        gMapView.onResume(); // needed to get the map to display immediately
        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return v;
    }


    @Override
    public void onMapReady(GoogleMap map) {
        gMap = map;
        gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
                LatLng(0, 0), 0));

        if (gMap != null) {
            gMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
            gMap.getUiSettings().setMyLocationButtonEnabled(true);        }
    }
    @Override
    public void onResume() {
        super.onResume();
        gMapView.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();
        gMapView.onPause();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        gMapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        gMapView.onLowMemory();
    }


} 

add_masjid.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#000000">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentStart="true"
        android:text="@string/xyz"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        />

    <Button
        android:id="@+id/generalId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/rect1"
        android:onClick="geoLocate"
        android:text="@string/abc"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/textView1"
        android:layout_toStartOf="@+id/generalId"
        android:ems="10"
        android:inputType="text"
        android:background="#FFFFFF"
        android:labelFor="@+id/editText1"
        android:layout_above="@+id/map"
        android:layout_alignParentTop="true" />

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/generalId">

    </com.google.android.gms.maps.MapView>

    <!--<fragment-->
        <!--android:id="@+id/map"-->
        <!--android:name="com.google.android.gms.maps.SupportMapFragment"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_alignParentBottom="true"-->
        <!--android:layout_below="@+id/generalId" />-->

</RelativeLayout>

请任何人指导我如何在我的应用程序中获取此按钮。

【问题讨论】:

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


    【解决方案1】:

    我已经通过在其中添加一些代码解决了我的问题,就像这样。

    @Override
        public void onMapReady(GoogleMap map) {
            gMap = map;
            gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
                    LatLng(0, 0), 0));
    
    
            if (gMap != null) {
                gMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
                if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext()
                        , Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext()
                        , Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    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;
                }
                gMap.setMyLocationEnabled(true);
                gMap.getUiSettings().setMyLocationButtonEnabled(true);
    
    
            }
        }
    

    【讨论】:

      【解决方案2】:

      这里有一些很好的教程来获取当前位置

      Link

      Link

      学习从这里获取当前位置,而不是像这样在此代码中更新位置参数 lat n lng。

       public void onMapReady(GoogleMap map) {
          gMap = map;
          gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
          gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
                  LatLng(lat, lng), 0));
      
          if (gMap != null) {
              gMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("Marker"));
              gMap.getUiSettings().setMyLocationButtonEnabled(true);        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-05
        • 2015-10-01
        • 2022-01-13
        相关资源
        最近更新 更多