Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在选项卡式活动中的SupportMapFragment上设置标记?_Android_Google Maps_Android Fragments_Google Maps Markers - Fatal编程技术网

Android 如何在选项卡式活动中的SupportMapFragment上设置标记?

Android 如何在选项卡式活动中的SupportMapFragment上设置标记?,android,google-maps,android-fragments,google-maps-markers,Android,Google Maps,Android Fragments,Google Maps Markers,我尝试在选项卡式活动中的片段内部实现支持映射片段 我试着设置一张地图,并在地图上设置标记。我成功地设置了地图,但没有设置标记。我试图找到答案,但我自己解决不了这个问题。我想得到一些帮助 地图片段代码: public class Map_frag extends SupportMapFragment implements OnMapReadyCallback, GoogleMap.OnMapClickListener { private SharedPreferences sp;

我尝试在选项卡式活动中的片段内部实现支持映射片段

我试着设置一张地图,并在地图上设置标记。我成功地设置了地图,但没有设置标记。我试图找到答案,但我自己解决不了这个问题。我想得到一些帮助

地图片段代码:

 public class Map_frag extends SupportMapFragment implements OnMapReadyCallback, GoogleMap.OnMapClickListener {

     private SharedPreferences sp;
     private Context context;
     private float myLat, myLng, locLat, locLnf;
     public GoogleMap map;
     private Marker marker;
     // Empty  constructor
     public Map_frag() {}

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.map_fragment, container,false);
        context = getContext();
        sp = PreferenceManager.getDefaultSharedPreferences(context);

        return v;
   }
   @Override
   public void onResume() {
        super.onResume();
        setUpMapIfNeeded();

   }

   private void setUpMapIfNeeded() {

        if (map == null) {
            getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap map) {

        this.map = map;
        onLocChange();
    }

    public void onLocChange(){
        if (map ==null) {
            map.clear();
            if (marker !=null){
                marker.remove();
            }
            myLat = Float.parseFloat(sp.getString("lat", "0"));
            myLng = Float.parseFloat(sp.getString("lng", "0"));
            LatLng latLng = new LatLng(myLat, myLng);
            marker = map.addMarker( new MarkerOptions().position(latLng).title("myLoc"));

            map.animateCamera(CameraUpdateFactory.
            newLatLngZoom(latLng,15));

        }
        return;
    }

    @Override
    public void onMapClick(LatLng latLng) {

    }
}
以及XML:

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

            <fragment android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:name="com.google.android.gms.maps.SupportMapFragment"
              android:id="@+id/fragment2"
              android:layout_gravity="center_horizontal"
              tools:layout="@layout/place_autocomplete_fragment"/>
    </LinearLayout>

onLocChange
这是什么
onLocationChanged
是一种覆盖方法。您可以使用它

注意:您已经创建了自己的
onLocChange
方法,并且您已经在
onMapReady上调用了它,因此它只在第一次调用时才会被调用。然后,
onLocChange
不会被调用。但是如果您使用了
onLocationChanged
override方法,则每次位置更改时都会调用它

里面

如果block覆盖了所有代码,并且只在map为null时有效,那么首先删除您的block。当map为null时,没有添加标记的意义,请将其远离。。或者像下面那样使用它

   if(map==null){
    //map null
    } else {
    // add code for markers 
    }
如果您使用上述块,则应与其他部分一起使用

 if (currLocationMarker != null) {
// you already have a marker so when location changes you remove it and add a new one thats why you need this 
                currLocationMarker.remove();
            }

 latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions(); // just added to clear thingsyou you can add this where instance creates only once 
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(iconForVehicleMarker);

        currLocationMarker = mGoogleMap.addMarker(markerOptions);
想要一个完整的例子,包括你需要知道的一切()?请参见如何在Mapready上更改
onLocation
以及此处使用的相关方法

import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

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.common.api.ResultCallback;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.PlaceLikelihood;
import com.google.android.gms.location.places.PlaceLikelihoodBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * An activity that displays a map showing places around the device's current location.
 */
public class MapsActivityCurrentPlaces extends AppCompatActivity implements
        OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    private static final String TAG = MapsActivityCurrentPlaces.class.getSimpleName();
    private GoogleMap mMap;
    private CameraPosition mCameraPosition;

    // The entry point to Google Play services, used by the Places API and Fused Location Provider.
    private GoogleApiClient mGoogleApiClient;
    // A request object to store parameters for requests to the FusedLocationProviderApi.
    private LocationRequest mLocationRequest;
    // The desired interval for location updates. Inexact. Updates may be more or less frequent.
    private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
    // The fastest rate for active location updates. Exact. Updates will never be more frequent
    // than this value.
    private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
            UPDATE_INTERVAL_IN_MILLISECONDS / 2;

    // A default location (Sydney, Australia) and default zoom to use when location permission is
    // not granted.
    private final LatLng mDefaultLocation = new LatLng(-33.8523341, 151.2106085);
    private static final int DEFAULT_ZOOM = 15;
    private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
    private boolean mLocationPermissionGranted;

    // The geographical location where the device is currently located.
    private Location mCurrentLocation;

    // Keys for storing activity state.
    private static final String KEY_CAMERA_POSITION = "camera_position";
    private static final String KEY_LOCATION = "location";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Retrieve location and camera position from saved instance state.
        if (savedInstanceState != null) {
            mCurrentLocation = savedInstanceState.getParcelable(KEY_LOCATION);
            mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
        }

        // Retrieve the content view that renders the map.
        setContentView(R.layout.activity_maps);
        // Build the Play services client for use by the Fused Location Provider and the Places API.
        buildGoogleApiClient();
        mGoogleApiClient.connect();
    }

    /**
     * Get the device location and nearby places when the activity is restored after a pause.
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (mGoogleApiClient.isConnected()) {
            getDeviceLocation();
        }
        updateMarkers();
    }

    /**
     * Stop location updates when the activity is no longer in focus, to reduce battery consumption.
     */
    @Override
    protected void onPause() {
        super.onPause();
        if (mGoogleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(
                    mGoogleApiClient, this);
        }
    }

    /**
     * Saves the state of the map when the activity is paused.
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (mMap != null) {
            outState.putParcelable(KEY_CAMERA_POSITION, mMap.getCameraPosition());
            outState.putParcelable(KEY_LOCATION, mCurrentLocation);
            super.onSaveInstanceState(outState);
        }
    }

    /**
     * Gets the device's current location and builds the map
     * when the Google Play services client is successfully connected.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        getDeviceLocation();
        // Build the map.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Handles failure to connect to the Google Play services client.
     */
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult result) {
        // Refer to the reference doc for ConnectionResult to see what error codes might
        // be returned in onConnectionFailed.
        Log.d(TAG, "Play services connection failed: ConnectionResult.getErrorCode() = "
                + result.getErrorCode());
    }

    /**
     * Handles suspension of the connection to the Google Play services client.
     */
    @Override
    public void onConnectionSuspended(int cause) {
        Log.d(TAG, "Play services connection suspended");
    }

    /**
     * Handles the callback when location changes.
     */
    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        updateMarkers();
    }

    /**
     * Manipulates the map when it's available.
     * This callback is triggered when the map is ready to be used.
     */
    @Override
    public void onMapReady(GoogleMap map) {
        mMap = map;

        // Turn on the My Location layer and the related control on the map.
        updateLocationUI();
        // Add markers for nearby places.
        updateMarkers();

        // Use a custom info window adapter to handle multiple lines of text in the
        // info window contents.
        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

            @Override
            // Return null here, so that getInfoContents() is called next.
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                // Inflate the layouts for the info window, title and snippet.
                View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents, null);

                TextView title = ((TextView) infoWindow.findViewById(R.id.title));
                title.setText(marker.getTitle());

                TextView snippet = ((TextView) infoWindow.findViewById(R.id.snippet));
                snippet.setText(marker.getSnippet());

                return infoWindow;
            }
        });
        /*
         * Set the map's camera position to the current location of the device.
         * If the previous state was saved, set the position to the saved state.
         * If the current location is unknown, use a default position and zoom value.
         */
        if (mCameraPosition != null) {
            mMap.moveCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition));
        } else if (mCurrentLocation != null) {
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(mCurrentLocation.getLatitude(),
                            mCurrentLocation.getLongitude()), DEFAULT_ZOOM));
        } else {
            Log.d(TAG, "Current location is null. Using defaults.");
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
            mMap.getUiSettings().setMyLocationButtonEnabled(false);
        }
    }

    /**
     * Builds a GoogleApiClient.
     * Uses the addApi() method to request the Google Places API and the Fused Location Provider.
     */
    private synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */,
                        this /* OnConnectionFailedListener */)
                .addConnectionCallbacks(this)
                .addApi(LocationServices.API)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .build();
        createLocationRequest();
    }

    /**
     * Sets up the location request.
     */
    private void createLocationRequest() {
        mLocationRequest = new LocationRequest();

        /*
         * Sets the desired interval for active location updates. This interval is
         * inexact. You may not receive updates at all if no location sources are available, or
         * you may receive them slower than requested. You may also receive updates faster than
         * requested if other applications are requesting location at a faster interval.
         */
        mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

        /*
         * Sets the fastest rate for active location updates. This interval is exact, and your
         * application will never receive updates faster than this value.
         */
        mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    /**
     * Gets the current location of the device and starts the location update notifications.
     */
    private void getDeviceLocation() {
        /*
         * Request location permission, so that we can get the location of the
         * device. The result of the permission request is handled by a callback,
         * onRequestPermissionsResult.
         */
        if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
                android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mLocationPermissionGranted = true;
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        }
        /*
         * Get the best and most recent location of the device, which may be null in rare
         * cases when a location is not available.
         * Also request regular updates about the device location.
         */
        if (mLocationPermissionGranted) {
            mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                    mLocationRequest, this);
        }
    }

    /**
     * Handles the result of the request for location permissions.
     */
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        mLocationPermissionGranted = false;
        switch (requestCode) {
            case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mLocationPermissionGranted = true;
                }
            }
        }
        updateLocationUI();
    }

    /**
     * Adds markers for places nearby the device and turns the My Location feature on or off,
     * provided location permission has been granted.
     */
    private void updateMarkers() {
        if (mMap == null) {
            return;
        }

        if (mLocationPermissionGranted) {
            // Get the businesses and other points of interest located
            // nearest to the device's current location.
            @SuppressWarnings("MissingPermission")
            PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
                    .getCurrentPlace(mGoogleApiClient, null);
            result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
                @Override
                public void onResult(@NonNull PlaceLikelihoodBuffer likelyPlaces) {
                    for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                        // Add a marker for each place near the device's current location, with an
                        // info window showing place information.
                        String attributions = (String) placeLikelihood.getPlace().getAttributions();
                        String snippet = (String) placeLikelihood.getPlace().getAddress();
                        if (attributions != null) {
                            snippet = snippet + "\n" + attributions;
                        }

                        mMap.addMarker(new MarkerOptions()
                                .position(placeLikelihood.getPlace().getLatLng())
                                .title((String) placeLikelihood.getPlace().getName())
                                .snippet(snippet));
                    }
                    // Release the place likelihood buffer.
                    likelyPlaces.release();
                }
            });
        } else {
            mMap.addMarker(new MarkerOptions()
                    .position(mDefaultLocation)
                    .title(getString(R.string.default_info_title))
                    .snippet(getString(R.string.default_info_snippet)));
        }
    }

    /**
     * Updates the map's UI settings based on whether the user has granted location permission.
     */
    @SuppressWarnings("MissingPermission")
    private void updateLocationUI() {
        if (mMap == null) {
            return;
        }

        if (mLocationPermissionGranted) {
            mMap.setMyLocationEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(true);
        } else {
            mMap.setMyLocationEnabled(false);
            mMap.getUiSettings().setMyLocationButtonEnabled(false);
            mCurrentLocation = null;
        }
    }
}
导入android.content.pm.PackageManager;
导入android.location.location;
导入android.os.Bundle;
导入android.support.annotation.NonNull;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.content.ContextCompat;
导入android.support.v7.app.AppActivity;
导入android.util.Log;
导入android.view.view;
导入android.widget.TextView;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.common.api.pendingreult;
导入com.google.android.gms.common.api.ResultCallback;
导入com.google.android.gms.location.LocationListener;
导入com.google.android.gms.location.LocationRequest;
导入com.google.android.gms.location.LocationServices;
导入com.google.android.gms.location.places.place;
导入com.google.android.gms.location.places.placeLikeLikeLihoodBuffer;
导入com.google.android.gms.location.places.places;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.CameraPosition;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;
导入com.google.android.gms.maps.model.MarkerOptions;
/**
*显示地图的活动,地图显示设备当前位置周围的位置。
*/
公共类MapsActivityCurrentPlaces扩展了AppCompativeActivity实现
四月一日,
GoogleAppClient.ConnectionCallbacks,
GoogleAppClient.OnConnectionFailedListener,
位置侦听器{
私有静态最终字符串标记=MapsActivityCurrentPlaces.class.getSimpleName();
私有谷歌地图;
私人摄像定位;
//由PlacesAPI和融合位置提供商使用的Google Play服务的入口点。
私人GoogleapClient MGoogleapClient;
//用于存储对FusedLocationProviderApi的请求参数的请求对象。
私人位置请求mLocationRequest;
//位置更新所需的间隔。不精确。更新的频率可能会增加或减少。
私有静态最终长更新间隔(以毫秒为单位)=10000;
//活动位置更新的最快速度。精确。更新的频率将永远不会更高
//大于此值。
私有静态最终长最快更新间隔(毫秒)=
更新间隔(单位:毫秒/2);
//默认位置(澳大利亚悉尼)和默认缩放,以在启用位置权限时使用
//不同意。
私人最终车床mDefaultLocation=新车床(-33.8523341151.2106085);
私有静态最终int默认值_ZOOM=15;
私有静态最终整数权限\请求\访问\精细\位置=1;
已授予私有布尔MLocationPermissions;
//设备当前所在的地理位置。
私有位置mCurrentLocation;
//用于存储活动状态的键。
专用静态最终字符串键\u CAMERA\u POSITION=“CAMERA\u POSITION”;
私有静态最终字符串KEY_LOCATION=“LOCATION”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//从保存的实例状态检索位置和相机位置。
如果(savedInstanceState!=null){
mCurrentLocation=savedInstanceState.getParcelable(键位置);
mCameraPosition=savedInstanceState.getParcelable(按键位置);
}
//检索渲染地图的内容视图。
setContentView(R.layout.activity_映射);
//构建Play services客户端,供融合位置提供程序和Places API使用。
buildGoogleAppClient();
mGoogleApiClient.connect();
}
/**
*暂停后恢复活动时,获取设备位置和附近位置。
*/
@凌驾
受保护的void onResume(){
super.onResume();
if(mgoogleapClient.isConnected()){
getDeviceLocation();
}
updateMarkers();
}
/**
*当活动不再处于焦点时停止位置更新,以减少电池消耗。
*/
@凌驾
受保护的void onPause(){
super.onPause();
if(mgoogleapClient.isConnected()){
LocationServices.FusedLocationApi.RemovelocationUpdate(
mGoogleApiClient,本);
}
}
/**
*当活动暂停时保存映射的状态。
*/
@凌驾
受保护的空位