0

我試圖實施Google Places自動填充API,但每當我搜索某些內容時,它的搜索欄都會在某些手機上崩潰。Google Places自動填充API在某些手機上崩潰

它可以在一些手機(如Moto G 2nd Generation XT1068)上工作,但是當我在搜索欄中搜索任何內容並按下回車鍵時,會在Lenovo A7000上崩潰。

我面對的另一個問題是;我想更改顯示南非附近的默認Google地圖位置。將其更改爲特定位置。我怎樣才能做到這一點?

package com.docto365.activity; 

import android.content.Context; 
import android.content.DialogInterface; 
import android.graphics.Color; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.provider.Settings; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 

import com.google.android.gms.common.api.Status; 
import com.google.android.gms.location.places.Place; 
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment; 
import com.google.android.gms.location.places.ui.PlaceSelectionListener; 
import com.google.android.gms.maps.CameraUpdate; 
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.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.LatLngBounds; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

import android.content.Intent; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 

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


import com.docto365.R; 

public class HospitalMapsActivity extends FragmentActivity /*implements OnMapReadyCallback*/ implements LocationListener{ 

    private GoogleMap mMap; 
    LocationManager location; 
    Toolbar mActionBarToolbar; 
    private static final LatLngBounds Bhubaneswar = new LatLngBounds(
      new LatLng(20.192597, 85.792239), new LatLng(20.409371, 85.824544)); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_hospital_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     /*SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this);*/ 
     PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) 
       getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 
     autocompleteFragment.setHint("Search Hospitals & Clinics"); 
     autocompleteFragment.setBoundsBias(Bhubaneswar); 
     mActionBarToolbar=(Toolbar)findViewById(R.id.toolbar); 
     mActionBarToolbar.setTitle("  Hospitals & Clinics"); 
     mActionBarToolbar.setNavigationIcon(R.drawable.ic_arrow_back); 
     mActionBarToolbar.setTitleTextColor(Color.WHITE); 
     mActionBarToolbar.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       finish(); 
      } 
     }); 
     location= (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     location.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 1000, this); 



     autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
      @Override 
      public void onPlaceSelected(Place place) { 
       // TODO: Get info about the selected place. 
       //Log.i(TAG, "Place: " + place.getName()); 
       //Toast.makeText(HospitalMapsActivity.this, "Place: " + place.getName(), Toast.LENGTH_LONG).show(); 
       //EditText et = (EditText) findViewById(R.id.autotext); 
       //String destination = et.getText().toString(); 
       String destination = (String) place.getName(); 

       List<Address> addressList = null; 
       if (destination == null || !destination.equals("")) { 
        Geocoder geocoder = new Geocoder(HospitalMapsActivity.this); 
        try { 
         addressList = geocoder.getFromLocationName(destination, 1); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        Address address = addressList.get(0); 
        LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude()); 
        mMap.addMarker(new MarkerOptions().position(latLng).title(destination)); 
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 
       } 
      } 

      @Override 
      public void onError(Status status) { 
       // TODO: Handle the error. 
       //Log.i(TAG, "An error occurred: " + status); 
       Toast.makeText(HospitalMapsActivity.this, "An error occured: " + status, Toast.LENGTH_LONG).show(); 
      } 
     }); 
     //setUpMapIfNeeded(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
    } 



    private void setUpMapIfNeeded() 
    { 
     if(mMap==null) 
      mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
     if(mMap!=null) 
      setUpMap(); 
    } 
    private void setUpMap() 
    { 
     //mMap.addMarker(new MarkerOptions().position(new LatLng(20.271790,85.843858)).title("Marker").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))); 
     mMap.setMyLocationEnabled(true); 
     //mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f)); 
     mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { 
      @Override 
      public boolean onMarkerClick(Marker marker) { 
       Toast.makeText(HospitalMapsActivity.this,"Tap on healthcare centre name to book a cab",Toast.LENGTH_LONG).show(); 
       return false; 
      } 
     }); 
     mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { 
      @Override 
      public void onInfoWindowClick(Marker marker) { 
       AlertDialog.Builder dialog = new AlertDialog.Builder(HospitalMapsActivity.this); 
       dialog.setTitle("Book Cab"); 
       dialog.setMessage("Do you want to book a cab?"/*HospitalMapsActivity.this.getResources().getString(R.string.gps_network_not_enabled)*/); 
       dialog.setPositiveButton("Book now"/*HospitalMapsActivity.this.getResources().getString(R.string.open_location_settings)*/, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
         // TODO Auto-generated method stub 
         Intent i = new Intent(HospitalMapsActivity.this, Book.class); 
         startActivity(i); 
         //get gps 
        } 
       }); 
       dialog.setNegativeButton("Later"/*HospitalMapsActivity.this.getResources().getString(R.string.open_location_settings)*/, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
         // TODO Auto-generated method stub 

         //get gps 
        } 
       }); 
       dialog.show(); 
      } 
     }); 


     LocationManager lm = (LocationManager)HospitalMapsActivity.this.getSystemService(Context.LOCATION_SERVICE); 
     boolean gps_enabled = false; 
     boolean network_enabled = false; 

     try { 
      gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     } catch(Exception ex) { 
      Toast.makeText(HospitalMapsActivity.this,"Something is wrong",Toast.LENGTH_LONG).show(); 
     } 

     try { 
      network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     } catch(Exception ex) { 
      Toast.makeText(HospitalMapsActivity.this,"Something is wrong",Toast.LENGTH_LONG).show(); 
     } 

     if(gps_enabled==false /*&& network_enabled==false*/) { 
      // notify user 
      AlertDialog.Builder dialog = new AlertDialog.Builder(HospitalMapsActivity.this); 
      dialog.setTitle("Location Services Disabled"); 
      dialog.setMessage("Please enable location services."/*HospitalMapsActivity.this.getResources().getString(R.string.gps_network_not_enabled)*/); 
      dialog.setPositiveButton("Enable"/*HospitalMapsActivity.this.getResources().getString(R.string.open_location_settings)*/, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
        // TODO Auto-generated method stub 
        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        HospitalMapsActivity.this.startActivity(myIntent); 
        //get gps 
       } 
      }); 

      dialog.show(); 
     } 



     mMap.addMarker(new MarkerOptions().position(new LatLng(20.260423, 85.777747)).title("AMRI HOSPITALS").icon(BitmapDescriptorFactory.fromResource(R.drawable.sp))); 

    } 

    @Override 
    public void onLocationChanged(Location arg0) { 
     LatLng latLng = new LatLng(arg0.getLatitude(), arg0.getLongitude()); 
     CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15.0f); 
     mMap.animateCamera(cameraUpdate); 
     location.removeUpdates(this); 






        setUpMapIfNeeded(); 
     //mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(20.269556,85.841157) , 14.0f)); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
} 
+0

歡迎使用堆棧溢出。請在時間發佈一個問題。嘗試在這裏收集關於您的錯誤的最多信息(這些手機的操作系統版本是什麼?) – technico

+1

Lenovo a7000具有v4.4.4,並且此手機中的搜索欄可讓應用程序停止。 Moto G2nd一代有v6.0.1,但這裏一切正常。 –

+0

你能夠添加崩潰的堆棧跟蹤嗎?這可能會幫助人們弄清楚發生了什麼問題。看着onPlaceSelected()我看到,addressList可以被設置之前被訪問(如果geocoder.getFromLocationName()拋出一個異常)。也許這就是發生了什麼事。 – AndrewR

回答

0

爲了解決你的問題:

它確實對聯想A7000在某些手機上工作(如Moto G的第二代XT1068),但崩潰每當我搜索在搜索欄中任何東西,然後按回車鍵。

請檢查以下內容:

  • 檢查,如果你的Android平臺包含在Android guide給予支持的版本。

  • 正如Google Places API - Troubleshooting中提到的那樣,您應用可能遇到的大部分錯誤通常都是由配置錯誤引起的。要獲得發生錯誤的狀態消息,請撥打PlaceAutocomplete.getStatus()回撥onActivityResult()

  • 在使用地方自動填充,請檢查是否自動完成被正確地添加到您的應用程序在Place Autocomplete給出了使用以下幾種方式:
    • 添加自動完成構件
    • 找地方預測編程

您也可以在此GitHub文章中檢查給定的解決方法 - Google Places Autocomplete does not work on mobile其中將相關代碼放入函數中,然後使用ng-focus指令到所需的元件上執行的表達,如:

<input id="searchBar" type="text" placeholder="Search" ng-focus="disableTap()"> 

我面對的另一個問題;我想更改顯示南非附近的默認Google地圖位置。將其更改爲特定位置。我怎樣才能做到這一點?

  • 要更改默認的谷歌地圖的位置,並得到精確的結果,嘗試在Update your location on Google給出的步驟,其中,您可以:
    • 打開你的位置或關閉,並
    • 檢查和更新您的位置

欲瞭解更多信息,請嘗試通過給定的文件。

我希望能爲你效勞。

相關問題