package com.example.myfastapp;import java.util.List;import java.util.Locale;import androID.app.Actionbar;import androID.app.AlertDialog;import androID.app.Dialog;import androID.content.Context;import androID.content.DialogInterface;import androID.content.IntentSender.SendIntentException;import androID.location.Address;import androID.location.Geocoder;import androID.location.Location;import androID.os.AsyncTask;import androID.os.Bundle;import androID.support.v4.app.Fragment;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.Imagebutton;import androID.Widget.linearLayout;import androID.Widget.TextVIEw;import com.Google.androID.gms.common.ConnectionResult;import com.Google.androID.gms.common.GooglePlayServicesUtil;import com.Google.androID.gms.common.API.Googleapiclient;import com.Google.androID.gms.common.API.PendingResult;import com.Google.androID.gms.common.API.ResultCallback;import com.Google.androID.gms.common.API.Status;import com.Google.androID.gms.location.LocationListener;import com.Google.androID.gms.location.LocationRequest;import com.Google.androID.gms.location.LocationServices;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.MapVIEw;import com.Google.androID.gms.maps.MapsInitializer;import com.Google.androID.gms.maps.SupportMapFragment;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;public class mapFragment extends Fragment implements LocationListener,Googleapiclient.ConnectionCallbacks,Googleapiclient.OnConnectionFailedListener { MapVIEw mapVIEw; //private VIEw touch; private GoogleMap map; private LatLng center; private LatLng currentpoint; private Geocoder geocoder; private List<Address> addresses; double latitude; double longitude; protected Context context; SupportMapFragment mf; VIEw v; private static final CharSequence[] MAP_TYPE_ITEMS = { "Road Map","Satellite","Terrain" }; // A request to connect to Location Services private LocationRequest mLocationRequest; private TextVIEw markerText,Address; private linearLayout markerLayout; private Googleapiclient mGoogleapiclient; boolean mUpdatesRequested = false; private GPSTracker gps; // Milliseconds per second public static final int MILliSECONDS_PER_SECOND = 1000; // The update interval public static final int UPDATE_INTERVAL_IN_SECONDS = 5; // A fast interval ceiling public static final int FAST_CEIliNG_IN_SECONDS = 1; // Update interval in milliseconds public static final long UPDATE_INTERVAL_IN_MILliSECONDS = MILliSECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS; // A fast ceiling of update intervals,used when the app is visible public static final long FAST_INTERVAL_CEIliNG_IN_MILliSECONDS = MILliSECONDS_PER_SECOND * FAST_CEIliNG_IN_SECONDS; public mapFragment(Context context) { super(); this.context = context; } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { v = inflater.inflate(R.layout.mapfragment,container,false);// v.setonDragListener(new MyDragListener()); mapVIEw = (MapVIEw)v.findVIEwByID(R.ID.mapvIEw); mapVIEw.onCreate(savedInstanceState); Imagebutton mapType = (Imagebutton) v.findVIEwByID(R.ID.mapType); mapType.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { showMapTypeSelectorDialog(); } }); Imagebutton myLocationCustombutton = (Imagebutton)v.findVIEwByID(R.ID.myLocationCustom); myLocationCustombutton.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { gps = new GPSTracker(getActivity()); gps.canGetLocation(); latitude = gps.getLatitude(); longitude = gps.getLongitude(); currentpoint = new LatLng(latitude,longitude); CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentpoint,18); map.animateCamera(cameraUpdate); map.setMyLocationEnabled(true); } }); markerText = (TextVIEw) v.findVIEwByID(R.ID.locationMarkertext); Address = (TextVIEw) v.findVIEwByID(R.ID.adresstext); markerLayout = (linearLayout) v.findVIEwByID(R.ID.locationMarker); // Getting Google Play availability status int status = GooglePlayServicesUtil .isGooglePlayServicesAvailable(context); if (status != ConnectionResult.SUCCESS) { // Google Play Services are // not available int requestCode = 10; Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status,getActivity(),requestCode); dialog.show(); } else { // Google Play Services are available // Getting reference to the SupportMapFragment // Create a new global location parameters object mLocationRequest = LocationRequest.create(); /* * Set the update interval */ mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILliSECONDS); // Use high accuracy mLocationRequest .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // Set the interval ceiling to one minute mLocationRequest .setFastestInterval(FAST_INTERVAL_CEIliNG_IN_MILliSECONDS); // Note that location updates are off until the user turns them on mUpdatesRequested = false; /* * Create a new location clIEnt,using the enclosing class to handle * callbacks. */ mGoogleapiclient = new Googleapiclient.Builder(getActivity()) .addAPI(LocationServices.API).addConnectionCallbacks(this) .addOnConnectionFailedListener(this).build(); mGoogleapiclient.connect(); } return v; } public voID toggleActionbar() { Actionbar ab = getActivity().getActionbar(); if (ab != null) { if (ab.isShowing()) { ab.hIDe(); } else { if(!ab.isShowing()) { ab.show(); } } } } private voID setupMap() { try { /*map = ((SupportMapFragment) this.getChildFragmentManager() .findFragmentByID(R.ID.map)).getMap();*/ map = mapVIEw.getMap(); // Enabling MyLocation in Google Map map.setMyLocationEnabled(true); map.getUiSettings().setZoomControlsEnabled(true); map.getUiSettings().setMyLocationbuttonEnabled(false); map.getUiSettings().setCompassEnabled(false); map.getUiSettings().setRotateGesturesEnabled(true); map.getUiSettings().setZoomGesturesEnabled(true); try { MapsInitializer.initialize(this.getActivity()); } catch (Exception e) { e.printstacktrace(); } PendingResult<Status> result = LocationServices.FusedLocationAPI .requestLocationUpdates(mGoogleapiclient,mLocationRequest,new LocationListener() { @OverrIDe public voID onLocationChanged(Location location) { markerText.setText("Location received: " + location.toString()); } }); Log.e("Reached","here"); result.setResultCallback(new ResultCallback<Status>() { @OverrIDe public voID onResult(Status status) { if (status.isSuccess()) { Log.e("Result","success"); } else if (status.hasResolution()) { // Google provIDes a way to fix the issue try { status.startResolutionForResult(getActivity(),100); } catch (SendIntentException e) { e.printstacktrace(); } } } }); gps = new GPSTracker(getActivity()); gps.canGetLocation(); latitude = gps.getLatitude(); longitude = gps.getLongitude(); currentpoint = new LatLng(latitude,longitude); Cameraposition cameraposition = new Cameraposition.Builder() .target(currentpoint).zoom(16f).tilt(30).bearing(90).build(); map.setMyLocationEnabled(true); map.animateCamera(CameraUpdateFactory .newCameraposition(cameraposition)); map.clear(); map.setonCamerachangelistener(new GoogleMap.OnCamerachangelistener() { @OverrIDe public voID onCameraChange(Cameraposition arg0) { // Todo auto-generated method stub //toggleActionbar(); center = map.getCameraposition().target; markerText.setText(" Set your Location "); map.clear(); markerLayout.setVisibility(VIEw.VISIBLE); try { new GetLocationAsync(center.latitude,center.longitude) .execute(); } catch (Exception e) { } } }); markerLayout.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub try { LatLng latLng1 = new LatLng(center.latitude,center.longitude); Marker m = map.addMarker(new MarkerOptions() .position(latLng1) .Title(" Set your Location ") .snippet("") .icon(BitmapDescriptorFactory .fromresource(R.drawable.my_location))); m.setDraggable(true); markerLayout.setVisibility(VIEw.GONE); } catch (Exception e) { } } }); } catch (Exception e) { e.printstacktrace(); } } @OverrIDe public voID onConnectionFailed(ConnectionResult result) { } @OverrIDe public voID onConnected(Bundle connectionHint) { setupMap(); } @OverrIDe public voID onConnectionSuspended(int cause) { } @OverrIDe public voID onLocationChanged(Location location) { } private class GetLocationAsync extends AsyncTask<String,VoID,String> { // boolean duplicateResponse; double x,y; StringBuilder str; public GetLocationAsync(double latitude,double longitude) { // Todo auto-generated constructor stub x = latitude; y = longitude; } @OverrIDe protected voID onPreExecute() { Address.setText(" Getting location "); } @OverrIDe protected String doInBackground(String... params) { try { geocoder = new Geocoder(context,Locale.ENGliSH); addresses = geocoder.getFromLocation(x,y,1); str = new StringBuilder(); if (Geocoder.isPresent()) { Address returnAddress = addresses.get(0); String localityString = returnAddress.getLocality(); String city = returnAddress.getCountryname(); String region_code = returnAddress.getCountryCode(); String zipcode = returnAddress.getPostalCode(); str.append(localityString + " "); str.append(city + " " + region_code + " "); str.append(zipcode + " "); } } catch (Exception e) { Log.e("tag",e.getMessage()); } return null; } @OverrIDe protected voID onPostExecute(String result) { try { Address.setText(addresses.get(0).getAddressline(0) + "," + addresses.get(0).getAddressline(1) + " "); } catch (Exception e) { e.printstacktrace(); } } @OverrIDe protected voID onProgressUpdate(VoID... values) { } } /* * public voID addMarker(double lati,double longi) { * * LatLng latlng = new LatLng(lati,longi); * * MarkerOptions mo = new MarkerOptions(); mo.position(latlng); * mo.icon(BitmapDescriptorFactory.fromresource(R.drawable.my_location)); * mo.Title("My Location:"+ latlng); map.addMarker(mo); * * //map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng,* 15));//prevIoUs code Cameraposition cameraposition = new * Cameraposition.Builder() .target(latlng) .zoom(11.0f) .bearing(90) // * OrIEntation of the camera to east .tilt(30) // Tilt of the camera to 30 * degrees .build(); * map.animateCamera(CameraUpdateFactory.newCameraposition(cameraposition)); * map.setMyLocationEnabled(true); * map.getUiSettings().setCompassEnabled(true); * map.getUiSettings().setZoomControlsEnabled(true); * //map.setMapType(GoogleMap.MAP_TYPE_norMAL); } */ public voID showMapTypeSelectorDialog() { // Prepare the dialog by setting up a Builder. final String fDialogTitle = "Select Map Type"; AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(fDialogTitle); // Find the current map type to pre-check the item representing the // current state. int checkItem = map.getMapType() - 1; System.out.print(checkItem); // Add an OnClickListener to the dialog,so that the selection will be // handled. builder.setSingleChoiceItems(MAP_TYPE_ITEMS,checkItem,new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog,int item) { // Locally create a finalised object. // Perform an action depending on which item was // selected. switch (item) { case 1: map.setMapType(GoogleMap.MAP_TYPE_SATELliTE); break; case 2: map.setMapType(GoogleMap.MAP_TYPE_TERRAIN); break; default: map.setMapType(GoogleMap.MAP_TYPE_norMAL); } dialog.dismiss(); } }); // Build the dialog and show it. AlertDialog fMapTypeDialog = builder.create(); fMapTypeDialog.setCanceledOntouchOutsIDe(true); fMapTypeDialog.show(); } @OverrIDe public voID onResume() { super.onResume(); mapVIEw.onResume(); mapVIEw.getMap(); } @OverrIDe public voID onPause() { super.onPause(); mapVIEw.onPause(); } @OverrIDe public voID onLowMemory() { super.onLowMemory(); mapVIEw.onLowMemory(); }}解决方法 onCamerachangelistener已被弃用,但好消息是谷歌已经发布了3个新的监听器.
见:GoogleMap.OnCameraChangeListener
This interface was deprecated.
Replaced by
GoogleMap.OnCameraMoveStartedListener
,GoogleMap.OnCameraMoveListener
andGoogleMap.OnCameraIDleListener
. The order in which the deprecatedonCameraChange
method will be called in relation to the methods in the new camera change Listeners is undefined.
我已经使用了这些听众,现在我可以在地图上获得保持和释放事件.
总结以上是内存溢出为你收集整理的android – 如何在触摸Uber等地图时切换 *** 作栏全部内容,希望文章能够帮你解决android – 如何在触摸Uber等地图时切换 *** 作栏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)