0

I am trying to create an app for a company that has a page that shows the nearest shop (since there are 36 shops in the country) I am using the Google Maps API and I don't know what to do this is my java class code:

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_page);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    LatLng Alexandra = new LatLng(-45.249167, 169.379722);
    mMap.addMarker(new MarkerOptions().position(Alexandra).title("Bin Inn Alexandra"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(Alexandra));
}}

and this is my xml code ignore the button that is where im going to put the shops info:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    tools:context=".MapsActivity" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
  • Take a look here https://stackoverflow.com/questions/32284708/how-to-constantly-detect-nearby-marker-locations-from-current-location-in-google – Chei Sep 09 '17 at 11:40

2 Answers2

0

There are couple of ways you can do this from user experience point of view. We have an app where we show the parking spots available for a user within his vicinity. Let's say there are 100 parking spots spread across the city. We decide upon the initial map zoom where we only show the parking spots which are say within 500 meters of his current position. As and when user zooms out, more and more spots shows up. Please note that we have already put marker all the parking spots, just that they are not shown.

Now I am not clear from your problem statement whether you want to show only the nearest shop on the map or what. In such a case, you either have to use something like a Google Distance Matrix API which takes the origin and destination lat and long and tells you the distance and duration. But if that's a overkill for you, employ something a lot simpler to calculate the distance between two points on a map and show the marker for the one with the least distance from the current position. Use this as reference - Calculate distance between two points in google maps V3

Community
  • 1
  • 1
Dibzmania
  • 1,934
  • 1
  • 15
  • 32
0

use this url to understand the code nearby places map api

And in a button onclick use this function

private String getUrl(double latitude, double longitude) {
    StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
    googlePlacesUrl.append("location=" + latitude + "," + longitude);
    googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
    googlePlacesUrl.append("&keyword=**companyname**");
    googlePlacesUrl.append("&sensor=true");
    googlePlacesUrl.append("&key=" + "API KEY");
    Log.d("getUrl", googlePlacesUrl.toString());
    return (googlePlacesUrl.toString());
}
Ashith VL
  • 89
  • 3
  • 14