Google地方信息自动填充API在部分手机上崩溃

时间:2016-08-06 17:30:20

标签: android android-studio search google-maps-api-3 google-places-api

我正在尝试实施Google商家信息自动填充API,但每当我搜索某些内容时,它的搜索栏就会在某些手机上崩溃。

它可以在某些手机上运行(如Moto G第二代XT1068),但只要我在搜索栏中搜索任何内容然后按回车键就会在联想A7000上崩溃。

我面临的另一个问题是;我想更改在南非附近显示的默认谷歌地图位置。将其更改为特定位置。我怎样才能做到这一点?

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) {

    }
}

1 个答案:

答案 0 :(得分:0)

解决您的问题:

它可以在某些手机上运行(如Moto G第二代XT1068),但只要我在搜索栏中搜索任何内容然后按回车键就会在联想A7000上崩溃。

请检查以下内容:

  • 检查您的Android平台是否包含在Android guide中提供的受支持版本中。

  • 正如Google Places API中提到的那样 - Troubleshooting,您的应用可能遇到的大多数错误通常都是由配置错误引起的。要获取发生错误的状态消息,请在PlaceAutocomplete.getStatus()回调中调用onActivityResult()

  • 在使用地方自动填充功能时,请使用以下方式检查自动填充是否已正确添加到您的应用中,如Place Autocomplete中所示:
    • 添加自动填充小部件
    • 以编程方式获取地点预测

您还可以检查此GitHub帖子中的给定变通方法 - Google Places Autocomplete does not work on mobile,其中相关代码已放入函数中,然后使用ng-focus指令在所需元素上执行表达式,如:< / p>

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

我面临的另一个问题是;我想更改在南非附近显示的默认谷歌地图位置。将其更改为特定位置。我怎么能这样做?

  • 要更改默认的Google地图位置并获得准确的结果,请尝试Update your location on Google中的步骤,其中您可以:
    • 打开或关闭您的位置,
    • 检查并更新您的位置

有关详细信息,请尝试浏览指定的文档。

我希望一个适合你。