如何在单击标记时显示选项列表?

时间:2016-07-26 18:55:37

标签: android list

我正在使用Android Google地图API创建一个应用程序,可以让我映射街道的辅助功能问题。 我的程序允许用户今天在地图上放置n个标记,搜索特定位置或将标记放在用户当前所在的位置(MyLocation按钮)。  我现在正在搜索如何在Android版Google地图API上创建列表。我们的想法是,当标记(放置在地图上的某个位置)被点击时,将出现一个列表,因此用户将能够选择此列表中的一个选项,并且将存储用户选择的值。选项表示用户找到的可访问性问题。 有没有人已经在这个API的某个地方使用了列表? 这是我的完整代码:

package com.example.i841838.myapplication; import android.app.Dialog; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v4.app.ActivityCompat; import android.support.v4.app.FragmentActivity; import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions;

import java.io.File;

import static com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable; import static com.google.android.gms.maps.CameraUpdateFactory.newLatLng;

public class MapsActivity extends FragmentActivity {

    GoogleMap googleMap;
    SharedPreferences sharedPreferences;
    int locationCount = 0;

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

// Getting Google Play availability status
        int status = isGooglePlayServicesAvailable(getBaseContext());

// Showing status
        if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();

        } else { // Google Play Services are available

// Getting reference to the SupportMapFragment of activity_main.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

// Getting GoogleMap object from the fragment
            googleMap = fm.getMap();

// Enabling MyLocation Layer of Google Map
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            googleMap.setMyLocationEnabled(true);

// Opening the sharedPreferences object
            sharedPreferences = getSharedPreferences("location", 0);

// Getting number of locations already stored
            locationCount = sharedPreferences.getInt("locationCount", 0);

// Getting stored zoom level if exists else return 0
            String zoom = sharedPreferences.getString("zoom", "0");

// If locations are already saved
            if(locationCount!=0){

                String lat = "";
                String lng = "";

// Iterating through all the locations stored
                for(int i=0;i<locationCount;i++){

// Getting the latitude of the i-th location
                    lat = sharedPreferences.getString("lat"+i,"0");

// Getting the longitude of the i-th location
                    lng = sharedPreferences.getString("lng"+i,"0");

// Drawing marker on the map
                    drawMarker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));
                }

// Moving CameraPosition to last clicked position
                googleMap.moveCamera(newLatLng(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng))));

// Setting the zoom level in the map on last position is clicked
                googleMap.animateCamera(CameraUpdateFactory.zoomTo(Float.parseFloat(zoom)));
            }

        }

        googleMap.setOnMapClickListener(new OnMapClickListener() {

            @Override
            public void onMapClick(LatLng point) {
                locationCount++;

// Drawing marker on the map
                drawMarker(point);

/** Opening the editor object to write data to sharedPreferences */
                SharedPreferences.Editor editor = sharedPreferences.edit();

// Storing the latitude for the i-th location
                editor.putString("lat"+ Integer.toString((locationCount-1)), Double.toString(point.latitude));

// Storing the longitude for the i-th location
                editor.putString("lng"+ Integer.toString((locationCount-1)), Double.toString(point.longitude));

// Storing the count of locations or marker count
                editor.putInt("locationCount", locationCount);

/** Storing the zoom level to the shared preferences */
                editor.putString("zoom", Float.toString(googleMap.getCameraPosition().zoom));

/** Saving the values stored in the shared preferences */
                editor.commit();

                Toast.makeText(getBaseContext(), "Marcador adicionado ao mapa!!!!", Toast.LENGTH_SHORT).show();

            }
        });

        googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {
            @Override
            public void onMapLongClick(LatLng point) {

// Removing the marker and circle from the Google Map
                googleMap.clear();

// Opening the editor object to delete data from sharedPreferences
                SharedPreferences.Editor editor = sharedPreferences.edit();

// Clearing the editor
                editor.clear();

// Committing the changes
                editor.commit();

// Setting locationCount to zero
                locationCount=0;

            }
        });

    }

    private void drawMarker(LatLng point){ // Creating an instance of MarkerOptions
        MarkerOptions markerOptions = new MarkerOptions();

// Setting latitude and longitude for the marker
        markerOptions.position(point);

// Adding marker on the Google Map
        googleMap.addMarker(markerOptions);
    }
     }

0 个答案:

没有答案