Back to project page MockLocation.
The source code is released under:
MIT License
If you think the Android project MockLocation listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.headdetect.android.mocklocation; // ww w . j a v a 2 s . c o m import android.app.Activity; import android.app.Fragment; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import com.headdetect.android.mocklocation.Tasks.AddressLookupTask; public class LocationMapFragment extends Fragment implements GoogleMap.OnMyLocationButtonClickListener, LocationListener, GoogleMap.OnMapClickListener{ private static final String ARG_SECTION_NUMBER = "section_number"; public static MarkerOptions currentMarker = null; private GoogleMap mMap; public LocationMapFragment() { } public static LocationMapFragment newInstance(int sectionNumber) { LocationMapFragment fragment = new LocationMapFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_map, container, false); setUpMapIfNeeded(); return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); ((MainActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER)); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. MapFragment mapFragment = ((MapFragment) this.getFragmentManager().findFragmentById(R.id.map)); mMap = mapFragment.getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } else { Log.e("MockLocation", "Could not get map"); } } } private void setUpMap() { mMap.setMyLocationEnabled(true); mMap.setOnMyLocationButtonClickListener(this); mMap.setOnMapClickListener(this); onMyLocationButtonClick(); } @Override public boolean onMyLocationButtonClick() { LocationManager locationManager = (LocationManager) getActivity().getSystemService(FragmentActivity.LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider); if (location != null) { onLocationChanged(location); } locationManager.requestLocationUpdates(provider, 20000, 0, this); return true; } @Override public void onLocationChanged(Location location) { if(mMap.getCameraPosition().zoom > 10) return; double lat = location.getLatitude(); double lng = location.getLongitude(); LatLng latlng = new LatLng(lat, lng); mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng)); mMap.animateCamera(CameraUpdateFactory.zoomTo(1)); onMapClick(latlng); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } @Override public void onMapClick(LatLng latLng) { MarkerOptions markerOption = new MarkerOptions(); markerOption.position(latLng); mMap.clear(); mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); mMap.addMarker(markerOption); currentMarker = markerOption; new AddressLookupTask(this.getActivity(), mMap, markerOption).execute(latLng); } }