2017-11-28 19 views
0

ユーザーからの入力場所(「病院」など)をMapsActivityに渡して、付近の病院を地図上に表示できるようにしたいとします。私は以下のようにそれを渡そうとしましたが失敗しました。なぜなら、私は文字列 "検索"のログメッセージを表示するので、ログメッセージはlogcatに表示することができません。他のログメッセージを表示することはできますが、「検索」のログメッセージのみを表示することはできません。それで、どうすればいいですか? 助けを得ることを望みます。ありがとう 以下は私のコードです。あなたのMapsActivityでMapsActivityにデータを渡すことができません

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

あなたは、この行を移動しようとしている:のonCreateメソッド上記

search = getIntent().getExtras().getString("search"); 

を、直後:

public class StationsMapsActivity extends FragmentActivity implements OnMapReadyCallback { 

これは、配置したonCreateメソッドだけでなく、クラス内の他のすべてのメソッドからアクセスできるようになると思います。

関連する問題