Android 获取要动态移动的位置标记

Android 获取要动态移动的位置标记,android,google-maps,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.LocationL

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

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) {

    }
}
package com.imran.googlemap;
导入android.app.Activity;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.support.v4.app.FragmentActivity;
导入android.os.Bundle;
导入android.widget.Toast;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.common.api.pendingreult;
导入com.google.android.gms.location.LocationRequest;
导入com.google.android.gms.location.LocationServices;
导入com.google.android.gms.location.LocationSettingsRequest;
导入com.google.android.gms.location.LocationSettingsResult;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
公共类MapsActivity扩展FragmentActivity实现GoogleAppClient.ConnectionCallbacks,GoogleAppClient.OnConnectionFailedListener,LocationListener{
私有GoogleMap mMap;//如果Google Play services APK不可用,则可能为空。
GoogleapClient MGoogleapClient;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
if(mGoogleAPIClient==null){
mGoogleAPIClient=new-GoogleApiClient.Builder(getApplicationContext()).addConnectionCallbacks(this.addOnConnectionFailedListener(this.addApi(LocationServices.API).build();
}
setupmapifneed();
}
@凌驾
受保护的void onStart(){
mGoogleAPIClient.connect();
super.onStart();
}
@凌驾
受保护的void onStop(){
mGoogleAPIClient.disconnect();
super.onStop();
}
@凌驾
受保护的void onResume(){
super.onResume();
}
私有void setUpMapIfNeeded()
{
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
//执行空检查以确认尚未实例化映射。
如果(mMap==null)
{
//尝试从SupportMapFragment获取映射。
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
//检查我们是否成功获得地图。
如果(mMap!=null)
{
mMap.setMyLocationEnabled(真);
}
}
}
@凌驾
未连接的公共空间(捆绑包){
Location mLastLocation=LocationServices.FusedLocationApi.getLastLocation(mgoogleapClient);
如果(mLastLocation==null){
双lat=mLastLocation.getLatitude();
double lng=mLastLocation.getLongitude();
Toast.makeText(getApplicationContext(),“lat”+String.valueOf(lat)+“Lng”+String.valueOf(Lng),Toast.LENGTH_LONG.show();
如果(mRequestingLocationUpdates){
startLocationUpdates();
}
}
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
}
受保护的void createLocationRequest(){
LocationRequest MLLocationRequest=新的LocationRequest();
mlLocationRequest.setInterval(10000);
mlLocationRequest.SetFastTestInterval(5000);
mLocationRequest.setPriority(位置请求.优先级高精度);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder()
.addLocationRequest(MLLocationRequest);
PendingResult=LocationServices.SettingsApi.checkLocationSettings(mgoogleAppClient,builder.build());
}
受保护的void startLocationUpdates(){
LocationServices.FusedLocationApi.RequestLocationUpdate(
mgoogleapclient,mLocationRequest,this);
}
@凌驾
已更改位置上的公共无效(位置){
mCurrentLocation=位置;
}
@凌驾
状态已更改的公共void(字符串s、int i、Bundle){
}
@凌驾
已提供已启用的公共void(字符串s){
}
@凌驾
公共无效onProviderDisabled(字符串s){
}
}

在onLocationChanged方法中设置您的标记,但是已经有很多错误。如果您可以编译它,就会更清楚。非常感谢。