Skip to content

Instantly share code, notes, and snippets.

@vaibhav-jani
Forked from joshdholtz/SomeFragment.java
Last active November 3, 2015 11:11
Show Gist options
  • Select an option

  • Save vaibhav-jani/13f431256fdaef37ce42 to your computer and use it in GitHub Desktop.

Select an option

Save vaibhav-jani/13f431256fdaef37ce42 to your computer and use it in GitHub Desktop.
Android Google Maps V2 - MapView in XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.gms.maps.MapView android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
package com.gostrive.strive.fragments.events;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
import com.androidquery.AQuery;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
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.model.LatLng;
import com.gostrive.strive.Constants;
import com.gostrive.strive.R;
import com.gostrive.strive.fragments.TileFragment;
import com.gostrive.strive.widget.SherlockMapFragment;
public class EventsMapFragment extends TileFragment implements LocationListener {
LocationManager lm;
Location location;
AQuery aq;
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_events_map, container, false);
lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
aq = new AQuery(v);
mapView = (MapView) aq.id(R.id.fragment_events_map_mapview).getView();
mapView.onCreate(savedInstanceState);
if (map == null) {
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.getActivity().registerReceiver(receiverLoadHome, new IntentFilter(Constants.BROADCAST_REFRESH_EVENTS));
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
this.getActivity().unregisterReceiver(receiverLoadHome);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onViewEntered(Intent intent) {
if (location != null) {
loadActivity();
} else {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
}
private void loadActivity() {
if (location != null) {
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10);
map.animateCamera(cameraUpdate);
}
}
BroadcastReceiver receiverLoadHome = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent data) {
loadActivity();
}
};
public void onLocationChanged(Location arg0) {
location = arg0;
lm.removeUpdates(this);
loadActivity();
Toast.makeText(getActivity(), "Location (" + arg0.getProvider() + ") - " + location.getLatitude() + "," + location.getLongitude(), Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment