不幸的是,使用谷歌地图关闭Android应用程序

时间:2015-03-31 18:51:45

标签: android maps

尝试通过谷歌地图在Android应用程序中输入名称搜索地点我的应用程序很遗憾被关闭。所以任何帮助都会有所帮助。

MainActivity.java file


import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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;

import java.io.IOException;
import java.util.List;


public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
MarkerOptions markerOptions;
LatLng latLng;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SupportMapFragment supportMapFragment = (SupportMapFragment)
            getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting a reference to the map
    googleMap = supportMapFragment.getMap();

    // Getting reference to btn_find of the layout activity_main
    Button btn_find = (Button) findViewById(R.id.btn_find);

    // Defining button click event listener for the find button
    OnClickListener findClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Getting reference to EditText to get the user input location
            EditText etLocation = (EditText) findViewById(R.id.et_location);

            // Getting user input location
            String location = etLocation.getText().toString();

            if(location!=null && !location.equals("")){
                new GeocoderTask().execute(location);
            }
        }
    };

    // Setting button click event listener for the find button
    btn_find.setOnClickListener(findClickListener);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}


// An AsyncTask class for accessing the GeoCoding Web Service
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{

    @Override
    protected List<Address> doInBackground(String... locationName) {
        // Creating an instance of Geocoder class
        Geocoder geocoder = new Geocoder(getBaseContext());
        List<Address> addresses = null;

        try {
            // Getting a maximum of 3 Address that matches the input text
            addresses = geocoder.getFromLocationName(locationName[0], 3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addresses;
    }


    @Override
    protected void onPostExecute(List<Address> addresses) {

        if(addresses==null || addresses.size()==0){
            Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
        }

        // Clears all the existing markers on the map
        googleMap.clear();

        // Adding Markers on Google Map for each matching address
        for(int i=0;i<addresses.size();i++){

            Address address = (Address) addresses.get(i);

            // Creating an instance of GeoPoint, to display in Google Map
            latLng = new LatLng(address.getLatitude(), address.getLongitude());

            String addressText = String.format("%s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                    address.getCountryName());

            markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title(addressText);

            googleMap.addMarker(markerOptions);

            // Locate the first location
            if(i==0)
                        googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        }
    }
}
  }

和我的activivity_main.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_find"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_btn_find"
        android:layout_alignParentRight="true" />

    <EditText
        android:id="@+id/et_location"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:hint="@string/hnt_et_location"
        android:layout_toLeftOf="@id/btn_find" />

</RelativeLayout>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="357dp"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    />

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="com.example.hp.restart.permission.MAPS_RECEIVE" />

<permission
    android:name="com.example.hp.restart.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBVIuUVtVswhShQtNDGQtcMxgX9SU"/>

</application>

因此,当我打开我的应用程序时,它的开放井谷歌地图也会出现在那里,但是当我在那里搜索时,我的应用程序被关闭,所以任何人都可以帮我找出

0 个答案:

没有答案