在谷歌地图上看不到多个标记

时间:2017-06-05 02:17:23

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

在PostExecute中我有这个数组名称:locations

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

from sklearn.feature_extraction.text import TfidfVectorizer
tf = TfidfVectorizer(stop_words = None, token_pattern='(?u)\\b\\w\\w*\\b')
​
words_list = ["歯","が","痛い"]
tfidf_matrix =  tf.fit_transform(words_list)
feature_names = tf.get_feature_names() 
print(feature_names)
# ['が', '歯', '痛い']

我尝试显示所有坐标

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

Google地图显示为空白。我怎么解决呢?

1 个答案:

答案 0 :(得分:0)

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

时为真
public void onMapReady(GoogleMap googleMap)

被召唤。

我会尽力让你开始:

您需要设置一些变量:

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

ArrayList将包含您从AsyncTask

获得的地点

然后,您需要在mapOnReady Methode中设置onCreate()

@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()将在数据和地图准备就绪时运行完毕。

我是在文本编辑器中写的 - 因此可能会出现一些语法错误。