如何找到我当前位置附近的位置?

2023-12-01

我需要一些关于使用 android“如何找到我所在位置附近的医院、学校、餐厅”的想法,怎么可能?


一步步,

Google place api用于访问附近的anloaction地标

Step 1:进入API控制台获取Place API

https://code.google.com/apis/console/

并在服务选项卡上选择

Service

就地服务

enter image description here

现在选择 API Access 选项卡并获取 API KEY

enter image description here

现在你有了一个 API 密钥来获取位置


现在正在编程中

*第2步 *:首先创建一个名为地方.java。该类用于包含 Place api 提供的 place 属性。

package com.android.code.GoogleMap.NearsetLandmark;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;


public class Place {
    private String id;
    private String icon;
    private String name;
    private String vicinity;
    private Double latitude;
    private Double longitude;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVicinity() {
        return vicinity;
    }

    public void setVicinity(String vicinity) {
        this.vicinity = vicinity;
    }

    static Place jsonToPontoReferencia(JSONObject pontoReferencia) {
        try {
            Place result = new Place();
            JSONObject geometry = (JSONObject) pontoReferencia.get("geometry");
            JSONObject location = (JSONObject) geometry.get("location");
            result.setLatitude((Double) location.get("lat"));
            result.setLongitude((Double) location.get("lng"));
            result.setIcon(pontoReferencia.getString("icon"));
            result.setName(pontoReferencia.getString("name"));
            result.setVicinity(pontoReferencia.getString("vicinity"));
            result.setId(pontoReferencia.getString("id"));
            return result;
        } catch (JSONException ex) {
            Logger.getLogger(Place.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public String toString() {
        return "Place{" + "id=" + id + ", icon=" + icon + ", name=" + name + ", latitude=" + latitude + ", longitude=" + longitude + '}';
    }

}

现在创建一个名为地点服务

package com.android.code.GoogleMap.NearsetLandmark;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;


public class PlacesService {

    private String API_KEY;

    public PlacesService(String apikey) {
        this.API_KEY = apikey;
    }

    public void setApiKey(String apikey) {
        this.API_KEY = apikey;
    }

    public List<Place> findPlaces(double latitude, double longitude,String placeSpacification) 
    {

        String urlString = makeUrl(latitude, longitude,placeSpacification);


        try {
            String json = getJSON(urlString);

            System.out.println(json);
            JSONObject object = new JSONObject(json);
            JSONArray array = object.getJSONArray("results");


            ArrayList<Place> arrayList = new ArrayList<Place>();
            for (int i = 0; i < array.length(); i++) {
                try {
                    Place place = Place.jsonToPontoReferencia((JSONObject) array.get(i));

                    Log.v("Places Services ", ""+place);


                    arrayList.add(place);
                } catch (Exception e) {
                }
            }
            return arrayList;
        } catch (JSONException ex) {
            Logger.getLogger(PlacesService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
//https://maps.googleapis.com/maps/api/place/search/json?location=28.632808,77.218276&radius=500&types=atm&sensor=false&key=<key>
    private String makeUrl(double latitude, double longitude,String place) {
         StringBuilder urlString = new StringBuilder("https://maps.googleapis.com/maps/api/place/search/json?");

        if (place.equals("")) {
                urlString.append("&location=");
                urlString.append(Double.toString(latitude));
                urlString.append(",");
                urlString.append(Double.toString(longitude));
                urlString.append("&radius=1000");
             //   urlString.append("&types="+place);
                urlString.append("&sensor=false&key=" + API_KEY);
        } else {
                urlString.append("&location=");
                urlString.append(Double.toString(latitude));
                urlString.append(",");
                urlString.append(Double.toString(longitude));
                urlString.append("&radius=1000");
                urlString.append("&types="+place);
                urlString.append("&sensor=false&key=" + API_KEY);
        }


        return urlString.toString();
    }

    protected String getJSON(String url) {
        return getUrlContents(url);
    }

    private String getUrlContents(String theUrl) 
    {
        StringBuilder content = new StringBuilder();

        try {
            URL url = new URL(theUrl);
            URLConnection urlConnection = url.openConnection();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()), 8);
            String line;
            while ((line = bufferedReader.readLine()) != null) 
            {
                content.append(line + "\n");
            }

            bufferedReader.close();
        }

        catch (Exception e)
        {

            e.printStackTrace();

        }

        return content.toString();
    }
}

现在创建一个新的活动,您想要在其中获取最近地点的列表。

/** * */

    package com.android.code.GoogleMap.NearsetLandmark;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.List;


    import android.app.AlertDialog;
    import android.app.ListActivity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Point;
    import android.graphics.drawable.Drawable;
    import android.location.Address;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.ContextMenu;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;

    import com.android.code.R;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapController;
    import com.google.android.maps.MapView;
    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapController;
    import com.google.android.maps.MapView;
    import com.google.android.maps.Overlay;

    /**
     * @author dwivedi ji     * 
     *        */
    public class CheckInActivity extends ListActivity {

    private String[] placeName;
    private String[] imageUrl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);




        new GetPlaces(this,getListView()).execute();
    }

    class GetPlaces extends AsyncTask<Void, Void, Void>{
        Context context;
        private ListView listView;
        private ProgressDialog bar;
        public GetPlaces(Context context, ListView listView) {
            // TODO Auto-generated constructor stub
            this.context = context;
            this.listView = listView;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            bar.dismiss();
              this.listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, placeName));

        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
              bar =  new ProgressDialog(context);
            bar.setIndeterminate(true);
            bar.setTitle("Loading");
            bar.show();


        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            findNearLocation();
            return null;
        }

    }
    public void findNearLocation()   {

        PlacesService service = new PlacesService("past your key");

       /* 
        Hear you should call the method find nearst place near to central park new delhi then we pass the lat and lang of central park. hear you can be pass you current location lat and lang.The third argument is used to set the specific place if you pass the atm the it will return the list of nearest atm list. if you want to get the every thing then you should be pass "" only   
       */

/* hear you should be pass the you current location latitude and langitude, */
          List<Place> findPlaces = service.findPlaces(28.632808,77.218276,"");

            placeName = new String[findPlaces.size()];
            imageUrl = new String[findPlaces.size()];

          for (int i = 0; i < findPlaces.size(); i++) {

              Place placeDetail = findPlaces.get(i);
              placeDetail.getIcon();

            System.out.println(  placeDetail.getName());
            placeName[i] =placeDetail.getName();

            imageUrl[i] =placeDetail.getIcon();

        }





    }


}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何找到我当前位置附近的位置? 的相关文章

  • 找不到参数的方法 dependencyResolutionManagement()

    我正在尝试使用老师给我的一个项目 但它显示了一个错误 Settings file Users admin AndroidStudioProjects HTTPNetworking settings gradle line 1 A probl
  • 卸载后 Web 应用程序不显示“添加到主屏幕”

    这是我第一次创建网络应用程序 我设法解决了这个问题 所以我得到了实际的 chrome 提示 将其添加到主屏幕 然后我从手机上卸载了该网络应用程序 因为我想将其展示给我的同事 但是 屏幕上不再出现提示 问题 这是有意为之的行为还是我的应用程序
  • 找不到 com.google.firebase:firebase-core:9.0.0 [重复]

    这个问题在这里已经有答案了 在遵循有些不一致的指示之后here https firebase google com docs admob android quick start name your project and here http
  • Android 后退按钮无法与 Flutter 选项卡内的导航器配合使用

    我需要在每个选项卡内有一个导航器 因此当我推送新的小部件时 选项卡栏会保留在屏幕上 代码运行得很好 但是 android 后退按钮正在关闭应用程序而不是运行 Navigator pop import package flutter mate
  • CardView 圆角获得意想不到的白色

    When using rounded corner in CardView shows a white border in rounded area which is mostly visible in dark environment F
  • 计数物体和更好的填充孔的方法

    我是 OpenCV 新手 正在尝试计算物体的数量在图像中 我在使用 MATLAB 图像处理工具箱之前已经完成了此操作 并在 OpenCV Android 中也采用了相同的方法 第一步是将图像转换为灰度 然后对其进行阈值计算 然后计算斑点的数
  • Android SIP 来电使用带有广播接收器的服务

    大家好 其实我正在尝试创建一个应用程序 支持基于 SIP 通过互联网进行音频呼叫 这里使用本机 sip 我遇到了来电问题 我已经完成了服务的注册部分 但是在接听电话时我无法接听电话 请帮助我 Service file package exa
  • Android MediaExtractor seek() 对 MP3 音频文件的准确性

    我在使用 Android 时无法在eek 上获得合理的准确度MediaExtractor 对于某些文件 例如this one http www archive org download emma solo librivox emma 01
  • 发布android后更改应用内购买项目的价格

    在 Google Play 上发布后 是否可以更改应用内购买商品的价格 我假设该应用程序也已发布 完整的在线文档位于http developer android com http developer android com也http sup
  • JavaMail 只获取新邮件

    我想知道是否有一种方法可以在javamail中只获取新消息 例如 在初始加载时 获取收件箱中的所有消息并存储它们 然后 每当应用程序再次加载时 仅获取新消息 而不是再次重新加载它们 javamail 可以做到这一点吗 它是如何工作的 一些背
  • 如何在PreferenceActivity中添加工具栏

    我已经使用首选项创建了应用程序设置 但我注意到 我的 PreferenceActivity 中没有工具栏 如何将工具栏添加到我的 PreferenceActivity 中 My code 我的 pref xml
  • 在 SQLite 中搜索时排除 HTML 标签和一些 UNICODE 字符

    更新 4 我已经成功运行了firstchar例如 但现在的问题是使用regex 即使包含头文件 它也无法识别regex操作员 有什么线索可以解决这个问题吗 更新 2 我已经编译了sqlite3我的项目中的库 我现在正在寻找任何人帮助我为我的
  • Android Studio 0.4.3 Eclipse项目没有gradle

    在此版本之前 在 Android Studio 中按原样打开 Eclipse 项目似乎很容易 无需任何转换 我更喜欢 Android Studio 环境 但我正在开发一个使用 eclipse 作为主要 IDE 的项目 我不想只为这个项目下载
  • 字符串数组文本格式化

    我有这个字符串 String text Address 1 Street nr 45 Address 2 Street nr 67 Address 3 Street nr 56 n Phone number 000000000 稍后将被使用
  • 我的设备突然没有显示在“Android 设备选择器”中

    我正在使用我的三星 Galaxy3 设备来测试过去两个月的应用程序 它运行良好 但从今天早上开始 当我将设备连接到系统时 它突然没有显示在 Android 设备选择器 窗口中 我检查过 USB 调试模式仅在我的设备中处于选中状态 谁能猜出问
  • 一次显示两条Toast消息?

    我希望在一个位置显示一条 Toast 消息 并在另一位置同时显示另一条 Toast 消息 多个 Toast 消息似乎总是按顺序排队和显示 是否可以同时显示两条消息 是否有一种解决方法至少可以提供这种外观并且不涉及扰乱活动布局 Edit 看来
  • 将 Intent 包装在 LabeledIntent 中以用于显示目的

    要求 我的应用程序中有一个 共享 按钮 我需要通过 Facebook 分享 我需要选择是否安装原生 Facebook 应用程序 我们的决定是 如果未安装该应用程序 则将用户发送到 facebook com 进行分享 当前状态 我可以检测何时
  • 捕获的图像分辨率太大

    我在做什么 我允许用户捕获图像 将其存储到 SD 卡中并上传到服务器 但捕获图像的分辨率为宽度 4608 像素和高度 2592 像素 现在我想要什么 如何在不影响质量的情况下获得小分辨率图像 例如我可以获取或设置捕获的图像分辨率为原始图像分
  • 将两个文本视图并排放置在布局中

    我有两个文本视图 需要在布局中并排放置 并且必须遵守两条规则 Textview2 始终需要完整显示 如果布局中没有足够的空间 则必须裁剪 Textview1 例子 文本视图1 文本视图2 Teeeeeeeeeeeeeeeeeextview1
  • Crashlytics 出现 Android Studio 构建错误

    我正在尝试将 CrashLytics 与 Android Studio 和 gradle 一起使用 但出现一个令人困惑的错误 java lang NoSuchMethodError 我的 build gradle 是 buildscript

随机推荐

  • Ajax 会话丢失

    我将 Symfony 应用程序从 Symfony 4 0 7 升级到 Symfony 4 1 之后 AJAX 调用会丢失会话值 我同时调用了大约 6 个 ajax 请求 第一个进展顺利 但其他人正在失去会话值 它仅在迁移到 Symfony
  • 用golang封装一个包

    想象一个导出一些结构和一些函数的包 如果我想围绕该包制作一个包装器 以便它可以用作插件 我是否应该重新创建嵌入旧结构的结构 例子 package foo type Foo struct Field string func DoSomethi
  • Google Fit API 配额和限制

    使用 google fit api 时是否有配额和请求限制 我想使用 google fit api 我很好奇使用它时是否有限制 您可以在以下位置检查您项目的 Fitness API 当前限制谷歌开发者控制台 我检查了当前的项目 默认限制是
  • Python - SqlAlchemy:按大圆距离过滤查询?

    我正在使用 Python 和 Sqlalchemy 在 Sqlite 数据库中存储纬度和经度值 我创建了一个混合法对于我的位置对象 hybrid method def great circle distance self other Tri
  • 导入变量命名空间

    是否可以使用这样的变量导入名称空间 namespace User Authorization Certificate use namespace 显然这不会运行use声明需要一个常量 但有解决方法吗 Edit 发现了一个 gem 仅适用于
  • Liferay 7 主题中的 jQuery 插件

    我需要一些帮助来理解 Liferay 7 主题 特别是使用 jQuery 插件 因为我遇到了与此线程相同的问题 https web liferay com community forums message boards view messa
  • 由于嵌套节点依赖关系,路径太长

    我正在使用 npm 来安装依赖项 安装完这些后 我想与非技术人员共享我的项目 并且没有 npm 所以我想在应用程序内发送 node modules 但是 由于节点嵌套了依赖项 因此它创建的文件具有很长的路径 217 个字符 node mod
  • 为什么 iTextSharp 中的 GetTextFromPage 返回的字符串越来越长?

    我正在使用最新的iTextSharpnuGet 5 5 8 中的 lib 用于解析 pdf 文件中的一些文本 我面临的问题是GetTextFromPage方法不仅返回应有的页面文本 还返回上一页的文本 这是我的代码 var url http
  • 通过 C# 代码执行 Powershell 命令

    我想通过 C 代码添加 Powershell 命令或脚本 什么是正确的 变量声明 默认值存储在 C 变量中 例如 在 Powershell 中我输入以下行 user Admin 我想在 C 代码中添加这一行 powershell AddSc
  • 在 Ubuntu 20.04 上安装 MySQL 时出现问题

    我正在尝试在 Ubuntu 20 04 中安装 MySQL 8 0sudo apt install mysql server 但是重新安装和使用后仍然出现此错误sudo dpkg configure a Setting up mysql s
  • 如何为一个类实例化更多 CDI bean?

    Note 类似的问题已经在三年前被问过 在 EE 6 的时候 请参阅如何为一个类实例化多个 CDI Weld bean 有什么变化吗EE 7 在 Spring 中 可以通过在 xml conf 中定义相应的 bean 来实例化任何类 也可以
  • PhoneGap 启动图片 iOS Apple Store 提交 [重复]

    这个问题在这里已经有答案了 一如既往地提交iTunesConnect of my PhoneGap申请起来比较麻烦 特别是当我尝试使用时 我看到弹出这条新消息Application Loader Your binary is not opt
  • 面向对象编程。从方法内部调用方法

    如何从类内的函数调用类方法 我的代码是 var myExtension init function Call onPageLoad onPageLoad function Do something 我试过了 onPageLoad 来自 in
  • 如何在Vue Material中设置灵活的网格

    我正在尝试构建一个使用 Vue Material 在网格中渲染用户输入的卡片的界面 卡片正确渲染 然而 我希望我的网格能够以消除间隙的方式弯曲 对齐和交错不同尺寸的卡片 如下所示 下面的代码与上面的网格相对应
  • PHP:使用来自 php 的参数调用 javascript 函数

    我正在尝试使用 PHP 变量参数调用 JavaScript 函数 我尝试了两种方法 在 PHP 中使用 echo 中的脚本标签调用 JavaScript 函数 IE 将 PHP 变量值赋给 JavaScript 变量
  • 为什么 Numba 不改进这个递归函数

    我有一个结构非常简单的真 假值数组 the real array has hundreds of thousands of items positions np array True False False False True True
  • 按组计算平均值

    我有一个类似于此的大型数据框 df lt data frame dive factor sample c dive1 dive2 10 replace TRUE speed runif 10 gt df dive speed 1 dive1
  • 安卓兼容包

    尝试使 Fragments 示例在低于 11 SDK 的版本上运行时 出现错误 setListAdapter new ArrayAdapter
  • 从数据绑定 DevExpress CheckedListBoxControl 获取项目索引

    我试图从以下位置找到特定值的索引选中列表框控件 CheckedListBoxControl 有一个 DataSource DisplayMember ValueMember 分别设置为 DataTable 和两列 现在我必须将 Checke
  • 如何找到我当前位置附近的位置?

    我需要一些关于使用 android 如何找到我所在位置附近的医院 学校 餐厅 的想法 怎么可能 一步步 Google place api用于访问附近的anloaction地标 Step 1 进入API控制台获取Place API https