Place Picker - Android


MapsActivity.java

package pickplaces.map.google.pickplaces;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.Map;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    UiSettings uisettings;
    int PLACE_PICKER_REQUEST = 1;
    Place place;
    SupportMapFragment mapFragment;
    String info;
    Marker marker;
    private Double lat = 29.00, lng = 80.00;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be 
used.       
 mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        try {
            startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }


    }


    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {
                place = PlacePicker.getPlace(data, this);
                //   String toastMsg = String.format("Place: %s", place.getName());  
              //   Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();          
      //   Place place = PlacePicker.getPlace(data, this);            
    String toastMsg = String
                        .format("Place: %s", place.getAddress());
                Toast.makeText(this, toastMsg,
                        Toast.LENGTH_LONG).show();
                lat = place.getLatLng().latitude;
                lng = place.getLatLng().longitude;
                info = place.getAddress().toString();
                putmarker(lat, lng, "Location Address", info, 3);
                cameraposition(1, lat, lng);
            }
        }
    }


    @Override    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mapoptions();
        mapsetting();


    }

    private void mapoptions() {
        GoogleMapOptions options = new GoogleMapOptions();
        options.compassEnabled(false).rotateGesturesEnabled(false)
                .mapToolbarEnabled(false).tiltGesturesEnabled(false)
                .zoomControlsEnabled(true);
        mapFragment.newInstance(options);
        mMap.setMapType(mMap.MAP_TYPE_NORMAL);
    }

    private void putmarker(Double lat, Double lng, String mtitle, String msnippet, 
int markerno) {

        marker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(lat, lng)).title(mtitle).snippet(msnippet)
                .draggable(true)
                .rotation(10)
        );
    }

    private void cameraposition(float i, double lat, double lng) {

        LatLng latlng = new LatLng(lat, lng);
        // googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latlng, 4));  
           CameraPosition cameraPosition = new CameraPosition.Builder()
           .target(latlng) // Center Set
           .zoom(18.0f) // Zoom  
           .bearing(i + 0) // Orientation of the camera to east        
            .tilt(30)
           .build(); // Creates a CameraPosition from the builder    
    mMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));
    }

    private void mapsetting() {
        uisettings = mMap.getUiSettings();
        uisettings.setZoomControlsEnabled(true);
        uisettings.setCompassEnabled(true);
        uisettings.setMyLocationButtonEnabled(true);
        uisettings.setScrollGesturesEnabled(true);
        uisettings.setZoomGesturesEnabled(false);
        uisettings.setTiltGesturesEnabled(true);
        uisettings.setRotateGesturesEnabled(true);
    }
}
Layout File: Activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"  
  xmlns:map="http://schemas.android.com/apk/res-auto"  
  xmlns:tools="http://schemas.android.com/tools"   
  android:id="@+id/map"   
  android:name="com.google.android.gms.maps.SupportMapFragment"  
  android:layout_width="match_parent"  
  android:layout_height="match_parent"   
  tools:context="pickplaces.map.google.pickplaces.MapsActivity" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="pickplaces.map.google.pickplaces">

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

    <application      
       android:allowBackup="true"    
       android:icon="@mipmap/ic_launcher"     
       android:label="@string/app_name"        
       android:supportsRtl="true"        
       android:theme="@style/AppTheme">


        <meta-data
            android:name="com.google.android.gms.version"            
            android:value="@integer/google_play_services_version" />
        <meta-data            
            android:name="com.google.android.geo.API_KEY"            
            android:value="AIzaSyCG1ucJwPFXTAJahYjpDn3ZotsQdI4eBg0" />

        <activity            
            android:name=".MapsActivity"            
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>



No comments:

Post a Comment