如何获取列表项目上的位置点击?

时间:2016-12-21 08:08:48

标签: android google-maps

This is my list of items as cities and on this activity on button click i want to show that item position in google map

我该怎么做?

这是我按下按钮点击“按地图显示” 公共类GooglePlacesActivity扩展FragmentActivity实现LocationListener {

private static final String GOOGLE_API_KEY = "AIzaSyBtft4Uif2qiDvz0uNYpSY6mrIoV6ZtH3g";
GoogleMap googleMap;
EditText placeText;
double latitude = 0;
double longitude = 0;
private int PROXIMITY_RADIUS = 5000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent newIntent = getIntent();
    int latitudeMinute = newIntent.getIntExtra("latitudeMinute", 10);
    int latitudeSecond = newIntent.getIntExtra("latitudeSecond", 20);
    int latitudeDegree = newIntent.getIntExtra("latitudeDegree", 30);
    int longitudeMinute = newIntent.getIntExtra("longitudeMinute", 40);
    int longitudeSecond = newIntent.getIntExtra("longitudeSecond", 50);
    int longitudeDegree = newIntent.getIntExtra("longitudeDegree", 16);
    float newlatitude = ((latitudeSecond / 60 + latitudeMinute) / 60 + latitudeDegree);
    float newlongitude = ((longitudeSecond / 60 + longitudeMinute) / 60 + longitudeDegree);
    //show error dialog if GoolglePlayServices not available
    if (!isGooglePlayServicesAvailable()) {
        finish();
    }
    setContentView(R.layout.activity_google_places);

    placeText = (EditText) findViewById(R.id.placeText);
    Button btnFind = (Button) findViewById(R.id.btnFind);
    SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
    googleMap = fragment.getMap();
    googleMap.setMyLocationEnabled(true);
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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;
    }
    Location location = locationManager.getLastKnownLocation(bestProvider);
    if (location != null) {
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);

    btnFind.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String type = placeText.getText().toString();
            StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
            googlePlacesUrl.append("location=" + latitude + "," + longitude);
            googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
            googlePlacesUrl.append("&types=" + type);
            googlePlacesUrl.append("&sensor=true");
            googlePlacesUrl.append("&key=" + GOOGLE_API_KEY);

            GooglePlacesReadTask googlePlacesReadTask = new GooglePlacesReadTask();
            Object[] toPass = new Object[2];
            toPass[0] = googleMap;
            toPass[1] = googlePlacesUrl.toString();
            googlePlacesReadTask.execute(toPass);
        }
    });
}

private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (ConnectionResult.SUCCESS == status) {
        return true;
    } else {
        GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
        return false;
    }
}


@Override
public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    LatLng latLng = new LatLng(latitude, longitude);
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(12));
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

0 个答案:

没有答案