【问题标题】:Map shows default marker but it is never set地图显示默认标记,但从未设置
【发布时间】:2016-09-23 15:05:39
【问题描述】:

map with default marker that is never set

在地图的中间有一个标记,当我启动带有地图片段的活动时,它总是显示在这个位置。但我从来没有设置这个标记...有人知道这是为什么,也许我可以删除这个标记吗?

这是我的代码:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    int permissionCheck = ContextCompat.checkSelfPermission(MapsActivity.this,
            android.Manifest.permission.ACCESS_FINE_LOCATION);

    if(permissionCheck == PackageManager.PERMISSION_GRANTED){
        getCurrentLocation();
        onSearch();
    }else if(permissionCheck == PackageManager.PERMISSION_DENIED){
        onSearch();
    }
}
//gets current location of the user.
public void getCurrentLocation() {
    double lat = 0;
    double lng = 0;
    try {
        mMap.setMyLocationEnabled(true); //allows query of current location.
    }catch(SecurityException e){
        e.printStackTrace();
    }

    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Geocoder geocoderGetAddress = new Geocoder(MapsActivity.this);
    try{
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if(location!=null) {
            lat = location.getLatitude();
            lng = location.getLongitude();
        }
    }catch(SecurityException e){
        e.printStackTrace();
    }
    //the rest of this method gets the address from the geocoder, so that it can be displayed as a String on the marker info window.
    String displayAddress ="";
    try {
        List<Address> currAddress = geocoderGetAddress.getFromLocation(lat, lng, 1);
        if(currAddress.size() >0){
            for(int i=0; i<currAddress.get(0).getMaxAddressLineIndex();i++){
                displayAddress += currAddress.get(0).getAddressLine(i) +"\n";
            }
        }
    }catch (IOException e){
        e.printStackTrace();
    }
    mMap.addMarker(new MarkerOptions().position(new LatLng(lat,lng)).title(myLoc).snippet(displayAddress));
    LatLng myLatLng = new LatLng(lat, lng);
    mMap.animateCamera(CameraUpdateFactory.newLatLng(myLatLng));
    initAcceptButton();
}

谢谢!

【问题讨论】:

  • 您将使用mMap.addMarker 添加它并使用mMap.animateCamera 将其动画化到中心
  • 不,我只是在获得智能手机的当前位置时添加这个标记。
  • 这并没有改变您添加它的位置,您当前的纬度/经度尚未修复。

标签: android dictionary marker


【解决方案1】:

我刚刚解决了添加的问题

if(lat!= 0 || lng != 0){
//add marker here
}

【讨论】:

    猜你喜欢
    • 2012-10-16
    • 1970-01-01
    • 2018-08-06
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    相关资源
    最近更新 更多