【问题标题】:Can't see multiple markers on google map在谷歌地图上看不到多个标记
【发布时间】:2017-11-05 17:57:21
【问题描述】:

在 PostExecute 我有这个数组名称:位置

我打印日志以获取更多详细信息:

[lat/lng: (18.5159983333333,-72.2916166666667), lat/lng: (18.5363383333333,-72.3231866666667), lat/lng: (18.5076266666667,-72.3164933333333), lat/lng: (18.54138,-72.2920766666667)]

我尝试显示所有坐标

protected void onPostExecute(Boolean result) {
            dialog.dismiss();

            for (int i = 0; i < locations.size(); i++)
            {

            Double latitude = Double.valueOf(locations.get(i).latitude);
            Double longitude = Double.valueOf(locations.get(i).longitude);

            LatLng lng = new LatLng(latitude,longitude);
            map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            map.addMarker(new MarkerOptions()
            .position(lng)
            .title("Test")
            .icon(BitmapDescriptorFactory.fromResource(getResources().getIdentifier("Title", "drawable", getPackageName()))));
            } 

        }

谷歌地图显示空白。请问如何解决?

【问题讨论】:

  • 你在哪里放置你的循环来添加标记?
  • 我的 AsyncTask 中的 onPostExecute
  • 我的意思是,你在哪里调用你的循环。在你的 onCreate(Bundle savedInstanceState) 内?
  • onPostExecute 在我的 AsyncTask 中。 AsyncTask 正在调用 onCreate(Bundle savedInstanceState)
  • 我刚刚为您添加了一些启动代码,如果您需要更多帮助,请告诉我。

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


【解决方案1】:

您只能在地图准备好接收标记时添加标记,并且在

public void onMapReady(GoogleMap googleMap)

被调用。

我会尝试让你开始这样做:

你需要设置一些变量:

private GoogleMap mMap;
private boolean isMapReady = false;
private ArrayList<LatLng> myLocations = null;

ArrayList 将包含您从AsyncTask 获得的位置

然后你需要在你的onCreate() Methode 中设置mapOnReady

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

    // What ever else you need

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

现在你需要重写onMapReady 方法:

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

    // Set a boolean flag to let other parts of you code  
    // know that the map is ready
    isMapReady = true;


    // If you need to get info from your marker Implement this
    //mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    //    @Override
    //    public boolean onMarkerClick(Marker marker) {

    //        showMarkerInfo(marker);
    //        return false;
    //    }
    //});


    //If you need a click listener add this 
    // Add implement the method 'onMapClickLocation'
    //mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    //    @Override
    //    public void onMapClick(LatLng latLng) {
    //        onMapClickLocation(latLng);
    //    }
    //});

    // call a function to update your locations
    updateMapLocations();
}

在你的(!)protected void onPostExecute(Boolean result) 中添加代码以填充myLocations ArrayList

protected void onPostExecute(Boolean result) {
        dialog.dismiss();
        myLocations = new ArrayList<LatLng>();

        for (int i = 0; i < locations.size(); i++)
        {

            Double latitude = Double.valueOf(locations.get(i).latitude);
            Double longitude = Double.valueOf(locations.get(i).longitude);

            LatLng lng = new LatLng(latitude,longitude);
            myLocations.put(lng);           
        } 

        // calls the update method when data is available
        this.updateMapLocations();
    }

并添加对updateMapLocations() 的调用,如下所示:

private void updateMapLocations(){
    // update locations won't continue unless the map is ready
    if(!isMapReady) return;

    // Zoom into your location !! if you need too
    // with a destinationLocation of your choice
    // perhaps your first location in you ArrayList
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(destinationLocation, zoomFactor));


    // PseudoCode!!
    // Add your for Loop
    // !!! Check to see if your myLocations ArrayList is null  before you continue !!! 
    // !!! or just add another boolean to you onPostExcecute method
    // And add the Markers

    //Loop Start
    //float hue = BitmapDescriptorFactory.HUE_MAGENTA;
    //MarkerOptions markerOptions = setMarker(latLng, hue)

    // !!! You need to add your destination loop !!!
    //mMap.addMarker(markerOptions);
    //Loop end!

}


    private MarkerOptions setMarker(LatLng latLng, float hue){
    MarkerOptions markerOptions = null;
    try {
        float markerAlpha = 0.7f;

        markerOptions = new MarkerOptions();
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(hue));
        markerOptions.position(latLng);
        markerOptions.alpha(markerAlpha);
        markerOptions.flat(false);
        markerOptions.title("What Ever");
        markerOptions.snippet("MySnippet");
        markerOptions.draggable(false);
        markerOptions.zIndex(0.0f);
    }
    catch (Exception ex){
        Log.e(TAG, "  setMarker --- " + ex.getMessage());
    }
    return markerOptions;
}

请注意,updateMapLocations() 在您的 onPostExecute()onMapReady() 方法中都被调用。 onPostExecute() 可能在调用 onMapReady() 之前完成。因此,首先当地图准备好时,updateMapLocations() 将在数据和地图准备好时运行完成。

我是在文本编辑器中编写的 - 所以可能会有一些语法错误。

【讨论】:

    猜你喜欢
    • 2013-11-08
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多