Android app getting unfortunately closed using google maps -
trying search places entering name in android app google maps app unfortunately getting closed. helpful.
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 reference map googlemap = supportmapfragment.getmap(); // getting reference btn_find of layout activity_main button btn_find = (button) findviewbyid(r.id.btn_find); // defining button click event listener find button onclicklistener findclicklistener = new onclicklistener() { @override public void onclick(view v) { // getting reference edittext 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 find button btn_find.setonclicklistener(findclicklistener); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } // asynctask class accessing geocoding web service private class geocodertask extends asynctask<string, void, list<address>>{ @override protected list<address> doinbackground(string... locationname) { // creating instance of geocoder class geocoder geocoder = new geocoder(getbasecontext()); list<address> addresses = null; try { // getting maximum of 3 address matches 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 existing markers on map googlemap.clear(); // adding markers on google map each matching address for(int i=0;i<addresses.size();i++){ address address = (address) addresses.get(i); // creating instance of geopoint, 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 first location if(i==0) googlemap.animatecamera(cameraupdatefactory.newlatlng(latlng)); } } } } and activivity_main.xml file
<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> so when open app opening google map gets appear there search there app gets closed unfortunately can please me find out
Comments
Post a Comment