简体   繁体   中英

How to use Google maps search functionality api in my application?

Is it possible to use it as library project for my application,i want to use Android Google Maps real app search-ability functionality. How can i do it,is it possible? Thanks in advance..

EDIT: I have shown Google Map in my app successfully, I want to include Google Map search functionality means that I can able to search any location in the world in auto suggested field and by selecting a particular location and move marker to that location. so how can I?

I tried this and this but not getting auto suggested text why I don't know..

I want like:

step1: show map with search box

在此输入图像描述

step2: while entering text it should auto suggest.

在此输入图像描述

step3: when click on particular name move map to that location

在此输入图像描述

You can easily provide that kind of search functionality by using Places API and Geocode API (Both will help you according to your usecase).

Read the below Documentation for your assistance.

  1. GeoCode API
  2. Places API

    I would recommend to use Places API for your need ( As per my observation on your usecase). But you could also use geocode, If you needed.

Many working reference and examples are there.

For startup, below are my reference :

  1. PlacesAPI AutoComplete feature , Hotel Finder with Autocomplete
  2. GeocodeAPI Simple GeoCoding

NOTE : I have suggested javascript API. But not sure whether it will help you in Android environment (I dont know anything about android environment).

No single Api can help you have to use multiple google api's

Step1. Implement Google Place autocomplete Read this

Step2. You have to geocode means you have to convert address to latitude and longitude check this

Step3. Now You can plot these lat-long on the map.

This works for me.

I think you should take a look at the Google Maps API for Android at https://developers.google.com/maps/documentation/android/

The Google Search Appliance doesn't have any mapping or geo search features right now.

This is how I did it ---

Android Manifest file should contain the following lines:

<uses-library
            android:name="com.google.android.maps"
            android:required="true" >
        </uses-library>

        <!-- You must insert your own Google Maps for Android API v2 key in here. -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="<put your api key value here>" />

Location XML file should have the following apart from anything extra:

 <fragment 
          android:name="com.google.android.gms.maps.SupportMapFragment"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>

Location java file should have something like this:

View mapView = null;
private GoogleMap mMap;
mMap = supportMapFragment.getMap();
mapView = (View) view.findViewById(R.id.map);

SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
                .findFragmentById(R.id.map);

         if(mMap != null){
            mMap.setMyLocationEnabled(true);
        }

    if(mMap != null)
            mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

                @Override
                public void onMapLongClick(LatLng latLng) {
                    new EditMap().execute("", String.valueOf(latLng.latitude), String.valueOf(latLng.longitude));
                }
            });


            class EditMap extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        /**
         * getting Albums JSON
         * */
        protected String doInBackground(String... args) {
            String address = args[0];
            double latitude = Double.parseDouble(args[1]);
            double longitude = Double.parseDouble(args[2]);

            return editMap(address, latitude, longitude);
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String result) {
            if(!result.equals(""))
                ToastUtil.ToastShort(getActivity(), result);
            else {
                mMap.clear();
                mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title(attvalue));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 11));
            }

        }
    }

NOTE:

These are the minimal requirements for the setting of location as you choose from Map that fills the location in your text.

There is a background thread that runs as you long press the location in a map. The listener defined for that is setOnMapLongClickListener as you see above.

The execution will place the marker to the exact location you chose to mark as set.

There will be a done button after you have chosen the location by a marker. This done button will confirm what you have chosen and will set that on a textfield for you.

The above code uses the method editMap to edit the map location.

The implementation is as done here:

private String editMap(String address, double latitude, double longitude ) {
        String keyword = null;

        try {
            Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
            if(!address.equals("")){
                keyword = address;
                java.util.List<android.location.Address> result = geocoder
                .getFromLocationName(keyword, 1);
                if (result.size() > 0) {
                    lat = (double) result.get(0).getLatitude();
                    lng = (double) result.get(0).getLongitude();
                    attvalue = address;
                } else {
                    return "Record not found";
                }
            } else {

                String sUrl = "http://google.com/maps/api/geocode/json?latlng="+latitude+","+longitude+"&sensor=true";

                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(sUrl);
                HttpResponse r = client.execute(get);
                int status = r.getStatusLine().getStatusCode();
                if(status == 200){
                    HttpEntity e = r.getEntity();
                    String data = EntityUtils.toString(e);

                    try{
                        JSONObject jsonObject = new JSONObject(data);
                        JSONArray results = jsonObject.getJSONArray("results");
                        JSONObject addressObject = results.getJSONObject(0);
                        JSONArray addressComp = addressObject.getJSONArray("address_components");

                        String city = "", state = "";
                        for(int i=0; i < addressComp.length(); i++){
                            JSONArray types = addressComp.getJSONObject(i).getJSONArray("types");
                            if(city.equals("") && types.getString(0).equals("locality"))
                                city = addressComp.getJSONObject(i).getString("long_name");
                            if(state.equals("") && types.getString(0).equals("administrative_area_level_1"))
                                state = addressComp.getJSONObject(i).getString("long_name");

                            if(!city.equals("") && !state.equals(""))
                                break;
                        }
                        attvalue = city + ", " + state;
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                    lat = latitude;
                    lng = longitude;
                }else{
                    return "Location Not Found";
                }
            }
        } catch (IOException io) {
            return "Connection Error";
        }
        return "";
    }

I hope this is enough to help you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM