有人可以帮我吗我试图搜索,但没有发现结果.
伟大的帮助将不胜感激!
这是我绘制的区号:
public class MainActivity extends FragmentActivity implements OntouchListener { private static final String TAG = "polygon"; private GoogleMap mGoogleMap; private VIEw mMapShelterVIEw; private GestureDetector mGestureDetector; private ArrayList<LatLng> mLatlngs = new ArrayList<LatLng>(); private polylineoptions mpolylineoptions; private polygonoptions mpolygonoptions; // flag to differentiate whether user is touching to draw or not private boolean mDrawFinished = false; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mMapShelterVIEw = (VIEw) findVIEwByID(R.ID.drawer_vIEw); mGestureDetector = new GestureDetector(this,new GestureListener()); mMapShelterVIEw.setontouchListener(this); initilizeMap(); //Contains(null); } private final class GestureListener extends SimpleOnGestureListener { @OverrIDe public boolean onDown(MotionEvent e) { return true; } @OverrIDe public boolean onFling(MotionEvent e1,MotionEvent e2,float veLocityX,float veLocityY) { return false; } } /** * Ontouch event will draw poly line along the touch points * */ @OverrIDe public boolean ontouch(VIEw v,MotionEvent event) { int X1 = (int) event.getX(); int Y1 = (int) event.getY(); Point point = new Point(); point.x = X1; point.y = Y1; LatLng firstGeoPoint = mGoogleMap.getProjection().fromScreenLocation( point); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: if (mDrawFinished) { X1 = (int) event.getX(); Y1 = (int) event.getY(); point = new Point(); point.x = X1; point.y = Y1; LatLng geoPoint = mGoogleMap.getProjection() .fromScreenLocation(point); mLatlngs.add(geoPoint); mpolylineoptions = new polylineoptions(); mpolylineoptions.color(color.RED); mpolylineoptions.wIDth(3); mpolylineoptions.addAll(mLatlngs); mGoogleMap.addpolyline(mpolylineoptions); **Log.d(TAG,"Latitude and longitude: " + mLatlngs);** } break; case MotionEvent.ACTION_UP: Log.d(TAG,"Poinnts array size " + mLatlngs.size()); mLatlngs.add(firstGeoPoint); mGoogleMap.clear(); mpolylineoptions = null; mMapShelterVIEw.setVisibility(VIEw.GONE); mGoogleMap.getUiSettings().setZoomGesturesEnabled(true); mGoogleMap.getUiSettings().setAllGesturesEnabled(true); mpolygonoptions = new polygonoptions(); mpolygonoptions.fillcolor(0x5500ff00); // mpolygonoptions.fillcolor(color.LTGRAY); mpolygonoptions.strokecolor(color.RED); mpolygonoptions.strokeWIDth(5); mpolygonoptions.addAll(mLatlngs); mGoogleMap.addpolygon(mpolygonoptions); mDrawFinished = false; break; } return mGestureDetector.ontouchEvent(event); } /** * Setting up map * */ private voID initilizeMap() { int status = GooglePlayServicesUtil .isGooglePlayServicesAvailable(getApplicationContext()); if (status == ConnectionResult.SUCCESS) { if (mGoogleMap == null) { mGoogleMap = ((SupportMapFragment) getSupportFragmentManager() .findFragmentByID(R.ID.map)).getMap(); mGoogleMap.setMyLocationEnabled(true); } } else if (GooglePlayServicesUtil.isUserRecoverableError(status)) { // showErrorDialog(status); } else { Toast.makeText(this,"No Support for Google Play Service",Toast.LENGTH_LONG).show(); } } /** * Method gets called on tap of draw button,It prepares the screen to draw * the polygon * * @param vIEw */ public voID drawZone(VIEw vIEw) { mGoogleMap.clear(); mLatlngs.clear(); mpolylineoptions = null; mpolygonoptions = null; mDrawFinished = true; mMapShelterVIEw.setVisibility(VIEw.VISIBLE); mGoogleMap.getUiSettings().setScrollGesturesEnabled(false); } public synchronized boolean Contains(Location location) { boolean isInsIDe = false; if (mLatlngs.size() > 0) { LatLng lastPoint = mLatlngs.get(mLatlngs.size() - 1); double x = location.getLongitude(); for (LatLng point : mLatlngs) { double x1 = lastPoint.longitude; double x2 = point.longitude; double dx = x2 - x1; if (Math.abs(dx) > 180.0) { if (x > 0) { while (x1 < 0) x1 += 360; while (x2 < 0) x2 += 360; } else { while (x1 > 0) x1 -= 360; while (x2 > 0) x2 -= 360; } dx = x2 - x1; } if ((x1 <= x && x2 > x) || (x1 >= x && x2 < x)) { double grad = (point.latitude - lastPoint.latitude) / dx; double intersectAtLat = lastPoint.latitude + ((x - x1) * grad); if (intersectAtLat > location.getLatitude()) isInsIDe = !isInsIDe; } lastPoint = point; } } return isInsIDe; }
我在mLatlang上有数组,现在我想靠近那个坐标不在当前位置的地方
解决方法 以下是使用Places Web Service API的一些工作代码,这应该可以帮助您获得所需的功能.一般文件可以找到here.
可以找到所有类型的地方类型here.
以下是一个简单的例子.首先,生成API的查询字符串:
public StringBuilder sbMethod() { //use your current location here double mLatitude = 37.77657; double mLongitude = -122.417506; StringBuilder sb = new StringBuilder("https://maps.GoogleAPIs.com/maps/API/place/nearbysearch/Json?"); sb.append("location=" + mLatitude + "," + mLongitude); sb.append("&radius=5000"); sb.append("&types=" + "restaurant"); sb.append("&sensor=true"); sb.append("&key=******* YOUR API KEY****************"); Log.d("Map","API: " + sb.toString()); return sb;}
以下是用于查询Places API的AsyncTask:
private class PlacesTask extends AsyncTask<String,Integer,String> { String data = null; // Invoked by execute() method of this object @OverrIDe protected String doInBackground(String... url) { try { data = downloadUrl(url[0]); } catch (Exception e) { Log.d("Background Task",e.toString()); } return data; } // Executed after the complete execution of doInBackground() method @OverrIDe protected voID onPostExecute(String result) { ParserTask parserTask = new ParserTask(); // Start parsing the Google places in JsON format // Invokes the "doInBackground()" method of the class ParserTask parserTask.execute(result); }}
这是downloadURL()方法:
private String downloadUrl(String strUrl) throws IOException { String data = ""; inputStream iStream = null; httpURLConnection urlConnection = null; try { URL url = new URL(strUrl); // Creating an http connection to communicate with url urlConnection = (httpURLConnection) url.openConnection(); // Connecting to url urlConnection.connect(); // Reading data from url iStream = urlConnection.getinputStream(); BufferedReader br = new BufferedReader(new inputStreamReader(iStream)); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = br.readline()) != null) { sb.append(line); } data = sb.toString(); br.close(); } catch (Exception e) { Log.d("Exception while downloading url",e.toString()); } finally { iStream.close(); urlConnection.disconnect(); } return data;}
用于解析JsON结果的ParserTask:
private class ParserTask extends AsyncTask<String,List<HashMap<String,String>>> { JsONObject jObject; // Invoked by execute() method of this object @OverrIDe protected List<HashMap<String,String>> doInBackground(String... JsonData) { List<HashMap<String,String>> places = null; Place_JsON placeJson = new Place_JsON(); try { jObject = new JsONObject(JsonData[0]); places = placeJson.parse(jObject); } catch (Exception e) { Log.d("Exception",e.toString()); } return places; } // Executed after the complete execution of doInBackground() method @OverrIDe protected voID onPostExecute(List<HashMap<String,String>> List) { Log.d("Map","List size: " + List.size()); // Clears all the existing markers; mGoogleMap.clear(); for (int i = 0; i < List.size(); i++) { // Creating a marker MarkerOptions markerOptions = new MarkerOptions(); // Getting a place from the places List HashMap<String,String> hmPlace = List.get(i); // Getting latitude of the place double lat = Double.parseDouble(hmPlace.get("lat")); // Getting longitude of the place double lng = Double.parseDouble(hmPlace.get("lng")); // Getting name String name = hmPlace.get("place_name"); Log.d("Map","place: " + name); // Getting vicinity String vicinity = hmPlace.get("vicinity"); LatLng latLng = new LatLng(lat,lng); // Setting the position for the marker markerOptions.position(latLng); markerOptions.Title(name + " : " + vicinity); markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_magenta)); // Placing a marker on the touched position Marker m = mGoogleMap.addMarker(markerOptions); } }}
ParserTask中使用的Place_JsON类:
public class Place_JsON { /** * Receives a JsONObject and returns a List */ public List<HashMap<String,String>> parse(JsONObject jObject) { JsONArray jplaces = null; try { /** RetrIEves all the elements in the 'places' array */ jplaces = jObject.getJsONArray("results"); } catch (JsONException e) { e.printstacktrace(); } /** Invoking getPlaces with the array of Json object * where each Json object represent a place */ return getPlaces(jplaces); } private List<HashMap<String,String>> getPlaces(JsONArray jplaces) { int placesCount = jplaces.length(); List<HashMap<String,String>> placesList = new ArrayList<HashMap<String,String>>(); HashMap<String,String> place = null; /** Taking each place,parses and adds to List object */ for (int i = 0; i < placesCount; i++) { try { /** Call getPlace with place JsON object to parse the place */ place = getPlace((JsONObject) jplaces.get(i)); placesList.add(place); } catch (JsONException e) { e.printstacktrace(); } } return placesList; } /** * Parsing the Place JsON object */ private HashMap<String,String> getPlace(JsONObject jplace) { HashMap<String,String> place = new HashMap<String,String>(); String placename = "-NA-"; String vicinity = "-NA-"; String latitude = ""; String longitude = ""; String reference = ""; try { // Extracting Place name,if available if (!jplace.isNull("name")) { placename = jplace.getString("name"); } // Extracting Place vicinity,if available if (!jplace.isNull("vicinity")) { vicinity = jplace.getString("vicinity"); } latitude = jplace.getJsONObject("geometry").getJsONObject("location").getString("lat"); longitude = jplace.getJsONObject("geometry").getJsONObject("location").getString("lng"); reference = jplace.getString("reference"); place.put("place_name",placename); place.put("vicinity",vicinity); place.put("lat",latitude); place.put("lng",longitude); place.put("reference",reference); } catch (JsONException e) { e.printstacktrace(); } return place; }}
最后,调用这个过程:
StringBuilder sbValue = new StringBuilder(sbMethod()); PlacesTask placesTask = new PlacesTask(); placesTask.execute(sbValue.toString());
结果:
总结以上是内存溢出为你收集整理的我正在Android中搜索附近的地方,例如银行,餐馆,ATM机在google地图上绘制的区域全部内容,希望文章能够帮你解决我正在Android中搜索附近的地方,例如银行,餐馆,ATM机在google地图上绘制的区域所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)