获取位置标记以动态移动

时间:2016-02-02 20:34:31

标签: android google-maps

我试图制作谷歌地图应用程序。地图标记将随设备移动动态移动。有很多错误,因为一个新的我无法正确。

package com.imran.googlemap;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener{

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    GoogleApiClient mGoogleAPIClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);


        if(mGoogleAPIClient==null){
            mGoogleAPIClient=new GoogleApiClient.Builder(getApplicationContext()).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
        }
        setUpMapIfNeeded();
    }


    @Override
    protected void onStart() {
        mGoogleAPIClient.connect();
        super.onStart();


    }

    @Override
    protected void onStop() {
        mGoogleAPIClient.disconnect();
        super.onStop();
    }

    @Override
    protected void onResume() {


        super.onResume();



    }

    private void setUpMapIfNeeded()
    {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);



        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null)
        {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null)
            {
                mMap.setMyLocationEnabled(true);

            }
        }
    }





    @Override
    public void onConnected(Bundle bundle) {
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleAPIClient);
        if(mLastLocation==null){
            double lat=mLastLocation.getLatitude();
            double lng=mLastLocation.getLongitude();
            Toast.makeText(getApplicationContext(),"lat "+String.valueOf(lat)+"Lng "+String.valueOf(lng),Toast.LENGTH_LONG).show();


            if (mRequestingLocationUpdates) {
                startLocationUpdates();
            }
        }
    }







    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    protected void createLocationRequest(){

        LocationRequest mLocationRequest= new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);
        PendingResult<LocationSettingsResult> result=LocationServices.SettingsApi.checkLocationSettings(mGoogleAPIClient, builder.build());
    }

    protected void startLocationUpdates() {
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleAPIClient, mLocationRequest, this);
    }

    @Override
    public void onLocationChanged(Location location) {
         mCurrentLocation = location;

    }


    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
}

0 个答案:

没有答案