Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LatLngParser
- {
- private final String API_KEY = "";
- public byte[] getUrlBytes(String urlSpec) throws IOException
- {
- URL url = new URL(urlSpec);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- try
- {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- InputStream in = connection.getInputStream();
- if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
- {
- throw new IOException(connection.getResponseMessage() +
- ": with " +
- urlSpec);
- }
- int bytesRead = 0;
- byte[] buffer = new byte[1024];
- while ((bytesRead = in.read(buffer)) > 0)
- {
- out.write(buffer, 0, bytesRead);
- }
- out.close();
- return out.toByteArray();
- } finally
- {
- connection.disconnect();
- }
- }
- public String getUrlString(String urlSpec) throws IOException
- {
- return new String(getUrlBytes(urlSpec));
- }
- public List<Coordinates> fetchItems(String address, String city)
- {
- List<Coordinates> items = new ArrayList<>();
- try
- {
- String url = Uri.parse("https://maps.googleapis.com/maps/api/geocode/json?address="
- + address
- + city
- + "PA&key="
- + API_KEY)
- .buildUpon()
- .build()
- .toString();
- String jsonString = getUrlString(url);
- JSONObject jsonBody = new JSONObject(jsonString);
- parseItems(items, jsonBody);
- } catch (JSONException je)
- {
- } catch (IOException ioe)
- {
- }
- return items;
- }
- private void parseItems(List<Coordinates> items, JSONObject jsonBody)
- throws IOException, JSONException
- {
- JSONArray results = jsonBody.getJSONArray("results");
- for(int i = 0; i < results.length(); i++)
- {
- Coordinates latlng = new Coordinates();
- JSONObject geometry = results.getJSONObject(i).getJSONObject("geometry");
- JSONObject location = geometry.getJSONObject("location");
- latlng.setLongitude(location.getDouble("lat"));
- latlng.setLatitude(location.getDouble("lng"));
- items.add(latlng);
- }
- }
- }
- public class MapFrag extends Fragment implements OnMapReadyCallback
- {
- private GoogleMap mGoogleMap;
- private MapView mapView;
- private Marker marker;
- private double lat;
- private double lng;
- private String city;
- private String address;
- private DatabaseReference mDatabase;
- public String getCity(String city)
- {
- return this.city = city;
- }
- public String getAddress(String address)
- {
- return this.address = address;
- }
- public double getLat(double latitude)
- {
- return this.lat = latitude;
- }
- public double getLng(double longitude)
- {
- return this.lng = longitude;
- }
- @Override
- public void onCreate(@Nullable Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- }
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
- {
- View v = inflater.inflate(R.layout.fragment_map, container, false);
- mapView = (MapView) v.findViewById(R.id.map);
- mapView.onCreate(savedInstanceState);
- mapView.onResume();
- mapView.getMapAsync(this);
- return v;
- }
- @Override
- public void onMapReady(final GoogleMap googleMap)
- {
- mGoogleMap = googleMap;
- mDatabase = FirebaseDatabase.getInstance().getReference();
- mDatabase.addValueEventListener(new ValueEventListener()
- {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot)
- {
- FetchItemsTask fetchItemsTask = new FetchItemsTask();
- for (DataSnapshot child : dataSnapshot.child("accounting").getChildren())
- {
- String name = child.child("Name").getValue().toString();
- String city = child.child("City").getValue().toString();
- String address = child.child("Address").getValue().toString();
- getCity(city);
- getAddress(address);
- fetchItemsTask.execute(address, city);
- Log.i("MAPS", "lat is " + lat + " lng is " + lng);
- LatLng markers = new LatLng(lat, lng);
- marker = mGoogleMap.addMarker(new MarkerOptions().position(markers).title(name));
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError)
- {
- }
- });
- }
- private class FetchItemsTask extends AsyncTask<String, Void, List<Coordinates>>
- {
- @Override
- protected List<Coordinates> doInBackground(String... params)
- {
- return new LatLngParser().fetchItems(params[0], params[0]);
- }
- @Override
- protected void onPostExecute(List<Coordinates> items)
- {
- getLat(Coordinates.getLatitude());
- getLng(Coordinates.getLongitude());
- }
- }
- }
- public class Coordinates implements Serializable
- {
- public static double Longitude;
- public static double Latitude;
- public static double getLongitude()
- {
- return Longitude;
- }
- public void setLongitude(double longitude)
- {
- this.Longitude = longitude;
- }
- public static double getLatitude()
- {
- return Latitude;
- }
- public void setLatitude(double latitude)
- {
- Latitude = latitude;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement