Created
December 8, 2016 07:02
-
-
Save anonymous/30d0a36ade565df0c9df9db1b67cf24e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.noavaran.drugstore.Fragment; | |
import android.location.Location; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
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.Toast; | |
import com.example.noavaran.drugstore.R; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.location.LocationListener; | |
import com.google.android.gms.location.LocationServices; | |
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.OnMapReadyCallback; | |
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; | |
/** | |
* A simple {@link Fragment} subclass. | |
*/ | |
public class MenuFragment extends Fragment { | |
MapView mMapView; | |
private GoogleMap googleMap; | |
Location mLastLocation; | |
public LatLng sydney; | |
public MenuFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
final View rootView = inflater.inflate(R.layout.fragment_menu, container, false); | |
mMapView = (MapView) rootView.findViewById(R.id.mapView); | |
mMapView.onCreate(savedInstanceState); | |
mMapView.onResume(); // needed to get the map to display immediately | |
try { | |
MapsInitializer.initialize(getActivity().getApplicationContext()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
mMapView.getMapAsync(new OnMapReadyCallback() { | |
@Override | |
public void onMapReady(GoogleMap mMap) { | |
googleMap = mMap; | |
// For showing a move to my location button | |
googleMap.setMyLocationEnabled(true); | |
// For dropping a marker at a point on the Map | |
/* googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description")). | |
setIcon(BitmapDescriptorFactory.fromResource(R.drawable.position_icon));*/ | |
googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { | |
@Override | |
public void onMyLocationChange(Location location) { | |
sydney=new LatLng(location.getLatitude(), location.getLongitude()); | |
googleMap.addMarker(new MarkerOptions().position(sydney).title("")) | |
.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.position_icon)); | |
CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build(); | |
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); | |
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { | |
@Override | |
public boolean onMarkerClick(final Marker marker) { | |
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); | |
// | |
LatLng markerPos=marker.getPosition(); | |
String markerLocationName=marker.getTitle(); | |
String markerSubCategoryName=marker.getSnippet(); | |
marker.remove(); | |
MarkerOptions markerOptions = | |
new MarkerOptions().position(markerPos) | |
.title(markerLocationName) | |
.snippet(markerSubCategoryName) | |
.icon(BitmapDescriptorFactory.fromResource(R.drawable.logo));// Changing marker icon | |
googleMap.addMarker(markerOptions); | |
/* MarkerOptions markerOptions = | |
new MarkerOptions().position(markerPos) | |
.title(markerLocationName) | |
.snippet(markerSubCategoryName) | |
.icon(BitmapDescriptorFactory.fromResource(R.drawable.logo)); | |
googleMap.addMarker(markerOptions); | |
Log.d("marker","change marker icon");*/ | |
return false; | |
} | |
}); | |
} | |
}); | |
// For zooming automatically to the location of the marker | |
/* */ | |
} | |
}); | |
return rootView; | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
mMapView.onResume(); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
mMapView.onPause(); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
mMapView.onDestroy(); | |
} | |
@Override | |
public void onLowMemory() { | |
super.onLowMemory(); | |
mMapView.onLowMemory(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment