Java tutorial
package com.jigarmjoshi; 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.Button; 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.LatLngBounds; import com.google.android.gms.maps.model.Marker; import com.jigarmjoshi.service.RefreshMapAsyncTask; import com.jigarmjoshi.utils.Utility; /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @author jigar.joshi * */ public class ActFragment extends Fragment { private MapView mapView; private static GoogleMap map; private View rootView; private Button refreshMapButton; public static GoogleMap getActGoogleMap() { return map; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_act, container, false); Utility.checkAndEnableLocation(container.getContext()); // Gets the MapView from the XML layout and creates it mapView = (MapView) rootView.findViewById(R.id.mapviewAct); refreshMapButton = (Button) rootView.findViewById(R.id.refreshMapButton); mapView.onCreate(savedInstanceState); // Gets to GoogleMap from the MapView and does initialization stuff map = mapView.getMap(); map.getUiSettings().setMyLocationButtonEnabled(true); // map.setMyLocationEnabled(true); mapView.refreshDrawableState(); map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { String title = marker.getTitle(); boolean validNumber = false; try { Long.parseLong(title); validNumber = true; } catch (Exception ex) { // this is current location marker } if (!validNumber) { return false; } Utility.createMapEntryDialog(rootView.getContext(), marker).show(); return false; } }); // Needs to call MapsInitializer before doing any CameraUpdateFactory // calls try { MapsInitializer.initialize(this.getActivity()); } catch (Exception ex) { Log.e(ActFragment.class.getName(), "failed to initialize map", ex); } Utility.focusAtCurrentLocation(map); refreshMapButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { refreshMapButton.setText("loading.."); refreshMapButton.refreshDrawableState(); refreshMapButton.setEnabled(false); LatLngBounds latLngBounds = map.getProjection().getVisibleRegion().latLngBounds; RefreshMapAsyncTask refreshMapAsyncTask = new RefreshMapAsyncTask(refreshMapButton, map, rootView.getContext(), mapView); refreshMapAsyncTask.execute(String.valueOf(latLngBounds.southwest.latitude), String.valueOf(latLngBounds.southwest.longitude), String.valueOf(latLngBounds.northeast.latitude), String.valueOf(latLngBounds.northeast.longitude)); } }); return rootView; } @Override public void onResume() { mapView.onResume(); super.onResume(); } @Override public void onPause() { super.onPause(); mapView.onPause(); } @Override public void onDestroy() { mapView.onDestroy(); super.onDestroy(); } @Override public void onLowMemory() { super.onLowMemory(); mapView.onLowMemory(); } }