【问题标题】:Adding Markers in Maps during app is running unsuccessful在应用程序运行期间在地图中添加标记不成功
【发布时间】:2017-08-26 17:47:32
【问题描述】:

我一直在尝试在应用程序运行时在我的谷歌地图活动中实现标记。有一些预定义的“位置”对象,并且在用户的“当前位置”处有一个标记。

使用SeekBar,我正在尝试更改应显示位置对象标记的当前位置的range,但是在更改SeekBar 时,没有任何反应(没有添加标记,当前位置只有一个)。为不需要的额外代码道歉,我相信最重要的部分是onConnected()onSeekBarChangedListener()onMapReady()

这是我的 MapsActivity.java:

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

    private GoogleApiClient googleApiClient;
    private SeekBar rangeSeekBar;
    private GoogleMap mMap;
    private Intent intent;
    private Location currentLocation;
    private Location[] locations;
    private double range;

    Location locationOne;
    Location locationTwo;
    Location locationThree;
    Location locationFour;
    Location locationFive;
    Location locationSix;
    Location locationSeven;

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

        locationOne = new Location("");
        locationTwo = new Location("");
        locationThree = new Location("");
        locationFour = new Location("");
        locationFive = new Location("");
        locationSix = new Location("");
        locationSeven = new Location("");

        locationOne.setLatitude(10.1399);
        locationOne.setLongitude(76.1784);

        locationTwo.setLatitude(10.2244);
        locationTwo.setLongitude(76.1978);

        locationThree.setLatitude(9.6175);
        locationThree.setLongitude(76.4301);

        locationFour.setLatitude(9.5987);
        locationFour.setLongitude(76.3116);

        locationFive.setLatitude(9.6175);
        locationFive.setLongitude(76.4301);

        locationSix.setLatitude(9.6737);
        locationSix.setLongitude(76.5610);

        locationSeven.setLatitude(15.5152);
        locationSeven.setLongitude(73.8565);

        locations = new Location[]{
            locationOne,
            locationTwo,
            locationThree,
            locationFour,
            locationFive,
            locationSix,
            locationSeven
        };

        rangeSeekBar = (SeekBar)findViewById(R.id.rangeSeekBar);
        rangeSeekBar.setProgress(0);

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

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

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        LatLng india = new LatLng(20.5937, 78.9629);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(india, 5));

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

        googleApiClient.connect();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

        if(ContextCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MapsActivity.this,new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},1);
        } else {
            currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

            LatLng currentLoc = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
            mMap.addMarker(new MarkerOptions().position(currentLoc).title("Your current location"));
        }

        addingMarkersInRange(0);
    }

    public void addingMarkersInRange(double range){
        for(int i=0;i<locations.length;i++){
            if(currentLocation.distanceTo(locations[i])<range) {
                LatLng tempLatLng = new LatLng(locations[i].getLatitude(), locations[i].getLongitude());
                mMap.addMarker(new MarkerOptions().position(tempLatLng));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(tempLatLng));
            }
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        addingMarkersInRange(rangeSeekBar.getProgress()*700000);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
} 

【问题讨论】:

  • 真的显示错误吗?还是没有显示标记?
  • @Kirankumar Zinzuvadia 是的,很抱歉,我的意思是没有显示标记。没有错误。

标签: android google-maps google-maps-markers marker


【解决方案1】:

您的addinMarkersInRange() 方法只是在locations 数组中添加LatLngs 的标记。因此,您需要在调用addingMarkersInRange 之前添加到locations 数组。但是,由于您无法添加到数组,请将位置更改为 List,您应该一切顺利。

您没有在 SeekBar 对象上设置监听器。请在你的 onCreate() 中设置OnSeekBarChangeListener

// code you already have
rangeSeekBar = (SeekBar)findViewById(R.id.rangeSeekBar);
rangeSeekBar.setProgress(0);

// register event listener here
rangeSeekBar.setOnSeekBarChangeListener(this);

【讨论】:

  • 但是位置数组不为空!我不太明白你...当我运行应用程序时,只有 1 个标记(在当前位置),没有别的。当我更改搜索栏时,甚至数组中的标记都没有显示出来..
  • @Ryuzaki 听起来很傻,但是您是否尝试放大,因为您的位置数组的 LatLng 彼此非常接近。
  • 好的,接下来就是使用 distanceTo() 计算距离。 distanceTo() 以米为单位返回距离。您如何确保范围以米为单位?
  • 嗯,我很困惑,这有什么关系? toDistance() 在 Meyers 中返回一个双精度值,我使用的是范围为 0-7000000 的搜索栏,所以......它只会将距离与搜索栏上的任何值进行比较......
  • @Ryuzaki 您错过了在 SeekBar 上设置 OnSeekBarChangeListener。请检查我的编辑以回答。
【解决方案2】:

我认为您在添加MarkersInRange() 时遇到问题。 但让我们以某种方式进行, 首先将侦听器设置为 seekbar。然后设置搜索栏的最大值。 现在最初调用 addMarkersInRange(1) 而不是传递零。

现在在搜索栏更改时使用 onProgress 更改中的 isfromUser 标志。

并删除 onCameraChange() 并将其放在 for 循环之外。并使用 latlongbound 为相机缩放级别设置动画。

现在运行并检查对 UI 所做的任何更改...!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    相关资源
    最近更新 更多