无法将数据传递到MapsActivity

时间:2017-11-28 01:15:23

标签: android

我希望将输入位置从用户(例如“医院”)传递到MapsActivity,以便可以在地图上显示附近的一些医院。我尝试传递它,如下所示,但失败了。因为当我显示字符串“search”的日志消息时,日志消息无法在logcat中显示。可以显示其他日志消息,但只能显示“搜索”的日志消息。那么,我怎么能通过呢? 希望得到一些帮助。谢谢 以下是我的代码。

MainActivity

 btnSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, MapsActivity.class);
            intent.putExtra("search", searchWord);
            startActivity(intent);
        }
    });

* MapsActivity

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


    //Log.e("halo:", search);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        checkLocationPermission();

    }
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    search = getIntent().getExtras().getString("search");
    mapFragment.getMapAsync(this);

    //setLocation();
}


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

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        bulidGoogleApiClient();
        mMap.setMyLocationEnabled(true);
        Log.e("check permission", ""+PackageManager.PERMISSION_GRANTED);
        Log.v("halo:", search);
    }

    setLocation();
}      

public void setLocation() {
    Object dataTransfer[] = new Object[2];
    GetNearbyPlacesData getNearbyPlacesData = new GetNearbyPlacesData();
    Log.e("set location", ""+dataTransfer);


    Log.e("halo:", search);

    if (search.equalsIgnoreCase("hospital")) {
        mMap.clear();
        String hospital = "hospital";
        String url = getUrl(latitude, longitude, hospital);
        dataTransfer[0] = mMap;
        dataTransfer[1] = url;

        getNearbyPlacesData.execute(dataTransfer);
        Toast.makeText(MapsActivity.this, "Showing Nearby Hospitals", Toast.LENGTH_SHORT).show();
    } else if (search.equalsIgnoreCase("clinic")) {
        mMap.clear();
        String url = getUrl(latitude, longitude, search);
        dataTransfer[0] = mMap;
        dataTransfer[1] = url;

        getNearbyPlacesData.execute(dataTransfer);
        Toast.makeText(MapsActivity.this, "Showing Nearby Clinics", Toast.LENGTH_SHORT).show();
    } else if (search.equalsIgnoreCase("pharmacy")) {
        mMap.clear();
        String url = getUrl(latitude, longitude, "pharmacy");
        dataTransfer[0] = mMap;
        dataTransfer[1] = url;

        getNearbyPlacesData.execute(dataTransfer);
        Toast.makeText(MapsActivity.this, "Showing Nearby Pharmacy", Toast.LENGTH_SHORT).show();
    } else if(search.equalsIgnoreCase("")) {
        Toast.makeText(this, "No location", Toast.LENGTH_SHORT).show();

    }
}


private String getUrl(double latitude , double longitude , String nearbyPlace)
{

    StringBuilder googlePlaceUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
    googlePlaceUrl.append("location="+latitude+","+longitude);
    googlePlaceUrl.append("&radius="+PROXIMITY_RADIUS);
    googlePlaceUrl.append("&type="+nearbyPlace);
    googlePlaceUrl.append("&sensor=true");
    googlePlaceUrl.append("&key="+"AIzaSyBLEPBRfw7sMb73Mr88L91Jqh3tuE4mKsE");

    Log.d("MapsActivity", "url = "+googlePlaceUrl.toString());

    return googlePlaceUrl.toString();
}

@Override
public void onConnected(@Nullable Bundle bundle) {

    locationRequest = new LocationRequest();
    locationRequest.setInterval(100);
    locationRequest.setFastestInterval(1000);
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);


    if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED)
    {
        LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
    }
}

0 个答案:

没有答案