Skip to content

Commit

Permalink
Locating zipcode on the map
Browse files Browse the repository at this point in the history
  • Loading branch information
sivakundana committed Apr 27, 2021
1 parent 8f97afe commit 075fdc5
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 83 deletions.
15 changes: 5 additions & 10 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.petneeds">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
Expand All @@ -13,12 +12,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PetNeeds">
<activity android:name=".DetailActivity"></activity>

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/map_key" />

<meta-data android:name="com.google.android.geo.API_KEY"
android:value="@string/map_key"/>
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
54 changes: 34 additions & 20 deletions app/src/main/java/com/example/petneeds/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SearchView;
import android.widget.Spinner;

Expand All @@ -31,7 +29,6 @@
import java.util.List;

import okhttp3.Headers;
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.OnMapReadyCallback;
Expand All @@ -40,12 +37,15 @@
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements OnMapReadyCallback,AdapterView.OnItemSelectedListener {


public static final String TAG = "MainActivity";
static Double lng;
static Double lat;
List<PetInfo> info;
private Spinner sp;
GoogleMap map;
SupportMapFragment mapFragment;
SearchView searchView;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -54,8 +54,10 @@ protected void onCreate(Bundle savedInstanceState) {


RecyclerView rvPetDetails = findViewById(R.id.rvPetDetails);
final EditText enteredZip = (EditText) findViewById(R.id.etZipcode);
ImageButton search = findViewById(R.id.searchbutton);
// final EditText enteredZip = (EditText) findViewById(R.id.etZipcode);
// ImageButton search = findViewById(R.id.searchbutton);
searchView = findViewById(R.id.sv_location);
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
//below is the code for setting up the drop down menu
sp = (Spinner) findViewById(R.id.drSpin);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
Expand Down Expand Up @@ -85,11 +87,25 @@ protected void onCreate(Bundle savedInstanceState) {
rvPetDetails.setLayoutManager(new LinearLayoutManager(this));
rvPetDetails.setAdapter(petAdapter);

search.setOnClickListener(new View.OnClickListener() {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public void onClick(View view) {
String value = enteredZip.getText().toString();
final String ZIPCODE = "https://maps.googleapis.com/maps/api/geocode/json?address=" + value + "&key=AIzaSyA3eIDiQk5MNmPvyx62qERmyb54UzORsIg";
public boolean onQueryTextSubmit(String query) {
String location = searchView.getQuery().toString();
final String ZIPCODE = "https://maps.googleapis.com/maps/api/geocode/json?address=" + location + "&key=AIzaSyA3eIDiQk5MNmPvyx62qERmyb54UzORsIg";
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(MainActivity.this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
map.addMarker(new MarkerOptions().position(latLng).title(location));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
}

final AsyncHttpClient client = new AsyncHttpClient();

client.get(ZIPCODE, new JsonHttpResponseHandler() {
Expand Down Expand Up @@ -123,9 +139,9 @@ public void onSuccess(int i, Headers headers, JSON json) {
} catch (JSONException e) {
e.printStackTrace();
}

}

@Override
public void onFailure(int i, Headers headers, String s, Throwable throwable) {

}
Expand All @@ -136,20 +152,23 @@ public void onFailure(int i, Headers headers, String s, Throwable throwable) {
public void onFailure(int i, Headers headers, String s, Throwable throwable) {

}

});
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);

mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
LatLng Dallas = new LatLng(32.789444, 96.791131);
map.addMarker(new MarkerOptions().position(Dallas).title("Dallas"));
map.moveCamera(CameraUpdateFactory.newLatLng(Dallas));
}

@Override
Expand All @@ -162,8 +181,3 @@ public void onNothingSelected(AdapterView<?> parent) {

}
}





101 changes: 55 additions & 46 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,80 @@
android:background="@drawable/gradient_background"
tools:context=".MainActivity">

<EditText
android:id="@+id/etZipcode"
android:layout_width="162dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="59dp"
android:hint="Enter zipcode" />
<!-- <EditText-->
<!-- android:id="@+id/etZipcode"-->
<!-- android:layout_width="162dp"-->
<!-- android:layout_height="45dp"-->
<!-- android:layout_alignParentStart="true"-->
<!-- android:layout_alignParentTop="true"-->
<!-- android:layout_marginStart="30dp"-->
<!-- android:layout_marginTop="59dp"-->
<!-- android:hint="Enter zipcode" />-->

<ImageButton
android:id="@+id/searchbutton"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="59dp"
android:layout_toEndOf="@+id/etZipcode"
android:src="@android:drawable/ic_menu_search" />
<!-- <ImageButton-->
<!-- android:id="@+id/searchbutton"-->
<!-- android:layout_width="40dp"-->
<!-- android:layout_height="45dp"-->
<!-- android:layout_alignParentTop="true"-->
<!-- android:layout_marginStart="0dp"-->
<!-- android:layout_marginTop="59dp"-->
<!-- android:layout_toEndOf="@+id/etZipcode"-->
<!-- android:src="@android:drawable/ic_menu_search" />-->


<com.google.android.gms.maps.MapView
android:id="@+id/mapView2"
android:layout_width="320dp"
android:layout_height="220dp"
android:layout_below="@+id/searchbutton"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="43dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="48dp"
android:background="#E6D3D3"
android:foregroundTint="#DCEBCD" />
<!-- <com.google.android.gms.maps.MapView-->
<!-- android:id="@+id/mapView2"-->
<!-- android:layout_width="320dp"-->
<!-- android:layout_height="220dp"-->
<!-- android:layout_below="@+id/searchbutton"-->
<!-- android:layout_alignParentStart="true"-->
<!-- android:layout_alignParentEnd="true"-->
<!-- android:layout_marginStart="43dp"-->
<!-- android:layout_marginTop="16dp"-->
<!-- android:layout_marginEnd="48dp"-->
<!-- android:background="#E6D3D3"-->
<!-- android:foregroundTint="#DCEBCD" />-->

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvPetDetails"
android:layout_width="316dp"
android:layout_height="353dp"
android:layout_below="@+id/mapView2"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="49dp"
android:layout_marginTop="13dp"
android:layout_marginTop="200dp"
android:layout_marginBottom="21dp" />


<TextView
android:id="@+id/label"
android:layout_width="173dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="41dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="69dp"
android:layout_marginStart="17dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="149dp"
android:layout_toEndOf="@+id/imageView2"

android:fontFamily="sans-serif-black"
android:text="PetNeeds"
android:textSize="24sp"
android:textStyle="bold" />

<SearchView
android:id="@+id/sv_location"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/bg_round"
android:elevation="5dp"
android:iconifiedByDefault="false"
android:queryHint="Enter a zip code" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="8dp"
Expand All @@ -80,22 +93,18 @@
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="320dp"
android:layout_height="220dp"
android:layout_below="@+id/etZipcode"
android:layout_alignParentStart="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="43dp"
android:layout_marginTop="18dp" />
android:layout_marginTop="110dp" />

<Spinner
android:id="@+id/drSpin"
android:layout_width="172dp"
android:layout_height="52dp"
android:layout_above="@+id/mapView2"
android:layout_alignStart="@+id/searchbutton"
android:layout_alignTop="@+id/searchbutton"
android:layout_marginStart="47dp"
android:layout_marginTop="14dp"
android:layout_marginBottom="-5dp"
android:layout_width="200dp"
android:layout_height="38dp"
android:layout_marginStart="250dp"
android:layout_marginTop="55dp"
android:layout_marginBottom="23dp"
android:visibility="visible"
tools:visibility="visible" />
</RelativeLayout>
6 changes: 0 additions & 6 deletions app/src/main/res/values/map_api.xml

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>
<string name="app_name">PetNeeds</string>
<!-- <string name="map_key" translatable="false">AIzaSyCRinuZCwJtyXYvMDE1S5Ex3cst6JJB_7c</string>-->
<string name="map_key" translatable="false">AIzaSyCRinuZCwJtyXYvMDE1S5Ex3cst6JJB_7c</string>
<string-array name="Categories">
<item>Pet Stores</item>
<item>Trainers</item>
Expand Down

0 comments on commit 075fdc5

Please sign in to comment.