Android 如何在谷歌地图上动态添加标记

Android 如何在谷歌地图上动态添加标记,android,google-maps,android-layout,android-fragments,google-api,Android,Google Maps,Android Layout,Android Fragments,Google Api,我已经在googlemap中创建并添加了一个标记,现在我将用户位置作为输入,并在用户位置的基础上添加另一个标记,并在两者之间画一条线 这就是我所做的 //OnCreate() mClient= new GoogleApiClient.Builder(this). addConnectionCallbacks(this). addOnConnectionFailedListener(this).

我已经在
googlemap
中创建并添加了一个标记,现在我将用户位置作为输入,并在用户位置的基础上添加另一个标记,并在两者之间画一条线

这就是我所做的

     //OnCreate()
     mClient= new GoogleApiClient.Builder(this).
                addConnectionCallbacks(this).
                addOnConnectionFailedListener(this).
                addApi(Places.GEO_DATA_API).enableAutoManage(this, this).build();




        mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

     /**
         *
         * @PLACES API
         */


        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                Log.i("Place Name", "Place: " + place.getName());
                LatLng latLng = place.getLatLng();
                destinationLat= latLng.latitude;
                destinationLng= latLng.longitude;


                destinationLatLng= new LatLng(destinationLat, destinationLng);
               /* mapFragment.addMarker(new MarkerOptions().position(destinationLatLng)
                        .title("PAF-KIET"));*/
            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Log.i("Error", "An error occurred: " + status);
            }
        });

   @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng pafKietLocation = new LatLng(24.865498, 67.073302);
        googleMap.addMarker(new MarkerOptions().position(pafKietLocation)
                .title("University"));
        googleMap.setMinZoomPreference(14.0f);
        googleMap.setMaxZoomPreference(74.0f);
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(pafKietLocation));

    }

问题是我无法找到任何方法将用户
LatLng
传递到地图

在您的活动中声明
GoogleMap
,并在
onMapReady(GoogleMap-GoogleMap)
中声明
add
this.map=GoogleMap
。当使用
this.map
时,始终检查空值。在
onPlaceSelected

if(this.map != null)
{
    this.map.addMarker(new MarkerOptions().position(destinationLatLng)
                    .title("PAF-KIET"));
}

在您的活动中声明
GoogleMap
,并在
onMapReady(GoogleMap-GoogleMap)
中添加
this.map=GoogleMap
。当使用
this.map
时,始终检查空值。在
onPlaceSelected

if(this.map != null)
{
    this.map.addMarker(new MarkerOptions().position(destinationLatLng)
                    .title("PAF-KIET"));
}

正如古尔根所说,使用谷歌地图的全局实例,然后在需要时使用它,但首先你应该在onCreate()方法中获取地图实例。

如古尔根所说,使用谷歌地图的全局实例,然后在需要时使用它,但首先你应该在onCreate()方法中获取地图实例。

“User Latling”,你是什么意思?@GurgenHakobyan:on
onMapReady
,我正在添加一个带有预定义
Lat Lng
值的标记,现在我还想在
onPlaceSelected()
上添加一个生成器。“User LatLng”,你是什么意思?@GurgenHakobyan:on
onMapReady
,我正在添加一个带有预定义
Lat Lng
值的标记,现在我还想在
onPlaceSelected()
上添加一个maker。