1

誰かがこれで私を助けることができるかどうか疑問に思っていました。Google Places API を使用して、アプリにオートコンプリートを追加しようとしています。私はチュートリアルに従っていますが、次のコードは私が問題を抱えているものです:

URL googlePlaces = new URL(
                "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" +
                URLEncoder.encode(args[0], "UTF-8") +
                "&types=geocode&language=en&sensor=true&key=" +
                getResources().getString(R.string.googleAPIKey));
                URLConnection tc = googlePlaces.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        tc.getInputStream()));

「R.string.googleAPIKey」が原因で、「getResources()」行が「タイプに解決できません」というエラーで返されます。以下にコード全体を入れます。

package com.multinav;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity {

    private GoogleMap googleMap;

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

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (googleMap == null){
            // Try to obtain the map from the SupportMapFragment.
            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (googleMap != null){
                setUpMap();
            }
        }

    }

    private void setUpMap() {
        // Enable MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

        // Get LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Create a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Get the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Get Current Location
        Location myLocation = locationManager.getLastKnownLocation(provider);

        // Set map type
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

        // Get latitude of the current location
        double latitude = myLocation.getLatitude();

        // get longitude of the current location
        double longitude = myLocation.getLongitude();

        // Create a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        // Show the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        //Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("BAAMMMM!"));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
public class Main extends Activity {
        /** Called when the activity is first created. */
    public ArrayAdapter<String> adapter;
    public AutoCompleteTextView textview;

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.item_list);
            final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list);
            final AutoCompleteTextView textView = (AutoCompleteTextView)
                    findViewById(R.id.autoCompleteTextView1);
            adapter.setNotifyOnChange(true);
            textView.setAdapter(adapter);
             textView.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (count%3 == 1) {
    adapter.clear();
                    GetPlaces task = new GetPlaces();
                            //now pass the argument in the textview to the task
                                    task.execute(textView.getText().toString());
        }
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {
    // TODO Auto-generated method stub

    }

    public void afterTextChanged(Editable s) {

    }

    });
}

    class GetPlaces extends AsyncTask<String, Void, ArrayList<String>> {

    @Override
                   // three dots is java for an array of strings
    protected ArrayList<String> doInBackground(String... args)
    {

    Log.d("gottaGo", "doInBackground");

    ArrayList<String> predictionsArr = new ArrayList<String>();


    try
    {

        URL googlePlaces = new URL(
                "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" +
                URLEncoder.encode(args[0], "UTF-8") +
                "&types=geocode&language=en&sensor=true&key= +
                getResources().getString(R.string.googleAPIKey));
                URLConnection tc = googlePlaces.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        tc.getInputStream()));

                String line;
                StringBuffer sb = new StringBuffer();
                                //take Google's legible JSON and turn it into one big string.
                while ((line = in.readLine()) != null) {
                sb.append(line);
                }
                                //turn that string into a JSON object
                JSONObject predictions = new JSONObject(sb.toString()); 
                               //now get the JSON array that's inside that object            
                JSONArray ja = new JSONArray(predictions.getString("predictions"));

                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = (JSONObject) ja.get(i);
                                        //add each entry to our array
                        predictionsArr.add(jo.getString("description"));
                    }
    } 
    catch (IOException e)
    {

    Log.e("MultiNAV", "GetPlaces : doInBackground", e);

    } 
    catch (JSONException e)
    {

    Log.e("MultiNAV", "GetPlaces : doInBackground", e);

    }

    return predictionsArr;

    }

    //then our post

    @Override
    protected void onPostExecute(ArrayList<String> result){

    Log.d("MultiNAV", "onPostExecute : " + result.size());
    //update the adapter
    adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.item_list);
    adapter.setNotifyOnChange(true);
    //attach the adapter to textview
    textview.setAdapter(adapter); 

    for (String string : result){

    Log.d("MultiNAV", "onPostExecute : result = " + string);
    adapter.add(string);
    adapter.notifyDataSetChanged();

    }

    Log.d("MultiNAV", "onPostExecute : autoCompleteAdapter" + adapter.getCount());


        }

    }

}

}
4

0 に答える 0