1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| import android.location.Location; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Toast; import com.google.android.gms.common.api.GoogleApiClient; 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.SupportMapFragment; import com.google.android.gms.maps.model.CameraPosition; import com.google.android.gms.maps.model.LatLng; import com.google.maps.android.clustering.Cluster; import com.google.maps.android.clustering.ClusterManager;
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List;
public class MyMap extends Fragment {
InputStream inputstream; BufferedReader reader; String title; private Location mCurrentLocation; AutoCompleteTextView actv; List<MyItem> items = new ArrayList<MyItem>(); String subtitle; String floor; String type; Double lat; Double lng; ArrayList<getmarkerfromstring> users=new ArrayList<getmarkerfromstring>(); String[] countries=new String[576]; int len; int arraylength; ClusterManager<MyItem> mClusterManager; JSONArray jsonArray; JSONObject jsonObject; String m; private GoogleMap mMap; private GoogleApiClient mGoogleApiClient;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) .addApi(LocationServices.API) .build(); View v=inflater.inflate(R.layout.mymap, container, false); actv=(AutoCompleteTextView)v.findViewById(R.id.autoCompleteTextView);
return v; }
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setUpMapIfNeeded();
}
@Override public void onResume() { super.onResume(); setUpMapIfNeeded(); }
private void setUpMapIfNeeded() { if (mMap == null) { mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null) { setUpMap(); } } }
private void initCamera() { CameraPosition position = CameraPosition.builder() .target(new LatLng(YOUR_INITIALIZING_LATITUDE, YOUR_INITIALIZING_LONGITUDE)) .zoom(18f) .bearing(0.0f) .tilt(40f) .build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), null);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); mMap.setTrafficEnabled(true); mMap.setMyLocationEnabled(true); mMap.getUiSettings().setZoomControlsEnabled(true);
}
private void setUpMap() { mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); initCamera(); mClusterManager = new ClusterManager<MyItem>(getActivity(), mMap); mMap.setOnCameraChangeListener(mClusterManager); mMap.setOnMarkerClickListener(mClusterManager);
mClusterManager .setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItem>() { @Override public boolean onClusterClick(final Cluster<MyItem> cluster) { mMap.animateCamera(CameraUpdateFactory.newLatLngZoom( cluster.getPosition(), (float) Math.floor(mMap .getCameraPosition().zoom + 2)), 300, null);
return true; } });
try { inputstream = getResources().getAssets().open("map.json"); reader = new BufferedReader(new InputStreamReader(inputstream)); m = reader.toString(); StringBuilder total = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { total.append(line); } m = total.toString(); } catch (IOException ex) { Toast.makeText(getActivity(), "fail", Toast.LENGTH_SHORT).show(); }
try {
jsonObject = new JSONObject(m); jsonArray = jsonObject.optJSONArray("map"); arraylength = jsonArray.length(); len = arraylength; for (int i = 0; i < arraylength; i++) {
JSONObject jsonChildNode = jsonArray.getJSONObject(i); title = jsonChildNode.optString("title"); subtitle = jsonChildNode.optString("subtitle"); floor = jsonChildNode.optString("floor"); type = jsonChildNode.optString("type"); lat = jsonChildNode.optDouble("latitude"); lng = jsonChildNode.optDouble("longitude"); double lat1=lat; double lng1=lng; items.add(new MyItem(lat1,lng1,title,subtitle,floor));
countries[2*i]=title; countries[2*i+1]=subtitle; getmarkerfromstring user = new getmarkerfromstring(); user.setLat(lat1); user.setLng(lng1); user.setName(countries[i]); users.add(user); getmarkerfromstring user2 = new getmarkerfromstring(); user2.setLat(lat1); user2.setLng(lng1); user2.setName(countries[2*i+1]); users.add(user2);
}
mClusterManager.addItems(items); mClusterManager.setRenderer(new MyClusterRenderer(getActivity(), mMap, mClusterManager)); mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() { @Override public boolean onClusterItemClick(MyItem myItem) { Toast.makeText(getActivity(), myItem.getfloor() + " floor",Toast.LENGTH_SHORT).show(); return false; } });
} catch (JSONException e) { e.printStackTrace(); }
ArrayAdapter<String> adapter= new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item,countries); actv.setAdapter(adapter);
actv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick (AdapterView<?> parent, View view, int position, long id) { String s=parent.getItemAtPosition(position).toString(); int pos= Arrays.asList(countries).indexOf(s); LatLng lng=new LatLng(users.get(pos).getLat(),users.get(pos).getLng()); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(lng,(float)Math.floor(mMap.getCameraPosition().zoom+8)));
} }); } }
|