“当前位置”按钮未显示

时间:2016-09-07 11:35:14

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

我在android app app(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>

请有人指导我如何在我的应用中获得此按钮。

2 个答案:

答案 0 :(得分:2)

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

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


        }
    }

答案 1 :(得分:0)

以下是获取当前位置的一些很好的教程

Link

Link

了解如何从此处获取当前位置,而不是像这样更新此代码中的位置参数。

 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);        }
}
相关问题