自定义 android 地图的地方?

Posted

技术标签:

【中文标题】自定义 android 地图的地方?【英文标题】:Customize the android maps places? 【发布时间】:2012-05-04 06:17:39 【问题描述】:

我想根据选择的区域(例如商家、收藏夹等)显示我的地点的地图。 我正在使用以下意图进行搜索并在 android 的地图中显示结果

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps"));
    startActivity(intent);

当我将地图地点点击为收藏时,它会在我的地点中显示该地点。

如何在Android中根据类别划分地点?

【问题讨论】:

【参考方案1】:

您应该使用 place api 来获取区域选择。

一步一步,

如何获取某个位置的最近地点列表。

第 1 步:前往 API 控制台获取 Place API

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

并在服务选项卡上选择

就地服务

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

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


现在正在编程中

*Step 2 *:首先创建一个名为Place.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 + '';
    


现在创建一个名为 PlacesService

的类
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=your_api_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();
    

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

/** * */

    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   
       */


          List<Place> findPlaces = service.findPlaces(28.632808,77.218276,"atm");
                                                      // Hear third argument, we pass the atm for getting atm , if you pass the hospital then this method return list of hospital , If you pass nothing then it will return all landmark 

            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();

        





    



【讨论】:

听说我传递了一个静态的滞后和位置纬度值 嗨 Ashish,我在第一个 csreen 中获取列表,当我点击列表位置时,如何在地图中显示位置? 这里我需要在第一个屏幕中提供搜索地图,然后在我需要显示列表之后 @user1365148 你应该在地图上点击获得经纬度。并在上面的代码中传递位置。这将提供当前位置列表 @user1365148 表明您应该更新您的问题。无论如何,我会为此提供完整的源代码.. 酷哥们!!!

以上是关于自定义 android 地图的地方?的主要内容,如果未能解决你的问题,请参考以下文章

Android 可视化走迷宫算法 支持自定义地图

如何在 Android 中显示自定义地图?

Android Studio 谷歌地图自定义地图,只想显示自定义地图但利用谷歌地图功能

android开发百度地图怎么实现自定义弹出窗口

Android高德地图自定义Markers的例子

Android Google Map API 自定义标记在自定义标记上带来谷歌地图数据