【问题标题】:How to add Marker Dynamically on Google Map如何在谷歌地图上动态添加标记
【发布时间】:2017-10-14 21:29:55
【问题描述】:

我在Google map 中创建并添加了一个标记,现在我将用户位置作为输入,并根据用户位置,我想添加另一个标记并在其间画一条线。

这就是我所做的。

     //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 传递到地图的任何方式。

【问题讨论】:

  • “用户LatLng”,你是什么意思?
  • @GurgenHakobyan :在onMapReady 上,我正在添加一个带有预定义Lat Lng 值的标记,现在我也想在onPlaceSelected() 上添加一个制造商。

标签: android google-maps android-layout android-fragments google-api


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    就像 Gurgen 所说,使用 GoogleMap 的全局实例,然后在需要时使用它,但首先你应该在 onCreate() 方法中获取地图实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 2020-03-31
      • 2019-09-23
      • 2016-03-16
      相关资源
      最近更新 更多