【问题标题】:How to add Google Map Markers outside of onMapReady() in Android?如何在 Android 中的 onMapReady() 之外添加 Google 地图标记?
【发布时间】:2015-10-12 19:06:03
【问题描述】:

我有以下函数可以返回设备的当前位置:

void getCurrentLocation()
{
    Location myLocation  = map.getMyLocation();
    if(myLocation!=null)
    {
        double dLatitude = myLocation.getLatitude();
        double dLongitude = myLocation.getLongitude();
        map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
                .title("My Location").icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));

    }
    else
    {
        Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
    }
}

但有些方法显示为红色,就像未定义一样:

如您所见,这些方法与地图相关,它在 onMapReady() 函数中工作,但在它之外显示它无法识别。这是为什么?我必须添加哪些库?我这样声明地图:

private MapFragment map;

【问题讨论】:

    标签: android google-maps google-maps-api-2


    【解决方案1】:

    这是您的一般代码结构应该是什么样子。 重要的部分是将您的本地 map 引用分配给 onMapReady() 回调中返回的引用。

    public class MainActivity extends Activity 
            implements OnMapReadyCallback {
    
        private GoogleMap map;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            MapFragment mapFragment = (MapFragment) getFragmentManager()
                    .findFragmentById(R.id.map);
    
            mapFragment.getMapAsync(this);
        }
    
        @Override
        public void onMapReady(GoogleMap retMap) {
    
            map = retMap;
    
            setUpMap();
    
        }
    
        public void setUpMap(){
    
            map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            map.setMyLocationEnabled(true);
        }
    
        void getCurrentLocation()
        {
            Location myLocation  = map.getMyLocation();
            if(myLocation!=null)
            {
                double dLatitude = myLocation.getLatitude();
                double dLongitude = myLocation.getLongitude();
                map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
                        .title("My Location").icon(BitmapDescriptorFactory
                                .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));
    
            }
            else
            {
                Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
            }
        }
    
    }
    

    【讨论】:

    • 我可以把getCurrentLocation()放在哪里?
    • @MuhammadShahzad 这种获取位置的方式已弃用,如果您想获取地图的当前位置,请看这里:stackoverflow.com/questions/30253123/…
    • 谢谢,我无法在 android studio 模拟器 5554 中获取当前位置,我使用了geo fix xxx xxxx,所有其他可能的方法都失败了。
    • @MuhammadShahzad 听起来你已经看到了:stackoverflow.com/questions/2279647/… 我建议使用真实设备来测试基于位置的代码.....
    【解决方案2】:

    你为什么要使用

     private MapFragment map;
    

    你的地图应该是类型

    com.google.android.gms.maps.GoogleMap
    

    改变一下

    private MapFragment map;
    

    private GoogleMap map;
    

    然后得到如下的地图:

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
    

    它会正常工作的。

    【讨论】:

    • google map 的声明方式是什么?我正在使用 map = (MapFragment) getFragmentManager().findFragmentById(R.id.map);分配这个,但我改为谷歌地图并给我一个错误,因为是不同的类型
    • @ReneLimon 更新了答案。检查最后一行。
    • 最后一个问题,getMap 没有被弃用?
    • @ReneLimon 是的,抱歉,它已被弃用。使用getMapAsync()onMapReady()返回地图对象,保证为not null
    • @karthik 实际上,它不能是 onMapAsync 因为我遵循 Daniel Nugent 步骤的不同类型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多