在谷歌地图android上显示当前位置

时间:2015-12-03 20:27:57

标签: java android google-maps

我知道有很多类似的问题,我读了很多,但没有一个对我有用。部分是因为谷歌地图sdk发生了变化。

我想要做的是向用户显示他在谷歌地图上的当前位置。

这就是我所做的:

在gradle文件中添加了以下依赖项:

compile 'com.google.android.gms:play-services:7.5.0'

在清单文件中添加了以下内容:

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

....


<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />

我片段的onCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mapFragment = (com.google.android.gms.maps.MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
    if (mapFragment == null){
        fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        mapFragment = com.google.android.gms.maps.MapFragment.newInstance();
        fragmentTransaction.replace(R.id.map, mapFragment).commit();
    }

    if (mapFragment != null){
        initilizeMap();
    }

    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_map, container, false);
}

initializeMap方法:

private void initilizeMap(){
    googleMap = mapFragment.getMap();

    if (googleMap != null){
        googleMap.getUiSettings().setAllGesturesEnabled(true);
        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        googleMap.getUiSettings().setCompassEnabled(true);

        googleMap.setOnMyLocationChangeListener(myLocationChangeListener());
    }
}

这是locationChangeListener应该给我当前的位置:

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener() {
    return new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());

            CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(15.0f).build();

            CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
            googleMap.moveCamera(cameraUpdate);
        }
}

发生的情况是地图出现但未显示当前位置。它显示了整个世界。

我错过了什么或做错了什么?提前谢谢。

1 个答案:

答案 0 :(得分:1)

首先,您必须设置Google Api客户端才能获取当前位置。 您可以在此处查看如何设置GoogleApiClient:

public class MainActivity extends AppCompatActivity  implements `GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener {`
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createLocationRequest();
        TextView mTextView = (TextView)findViewById(R.id.mTextView);
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buildGoogleApipClient();
                mGoogleApiClient.connect();


            }
        });

    }

    protected synchronized void buildGoogleApipClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(this, "Yes calling", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConnected(Bundle bundle) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if(mLastLocation!=null){
            Toast.makeText(this,mLastLocation.getLatitude()+"-----"+mLastLocation.getLongitude(),Toast.LENGTH_LONG).show();
        }
        if (true) {
            startLocationUpdates();
        }


    }