如何在地图的Android当前的位置设置的GeoPoint? [英] How to set geopoint on current location on map in android?
本文介绍了如何在地图的Android当前的位置设置的GeoPoint?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
*的的请告诉我如何设置地理点的当前位置在节目的开始?我想在开始标记为你在这里的程序设置覆盖气囊用户当前位置的仿真器。我的code是在这里 - 的*
包com.jbb;
进口的java.util.List;
进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapController;
进口com.google.android.maps.MapView;
进口com.google.android.maps.Overlay;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Point;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.view.MotionEvent;
进口android.widget.Toast;
公共类jbb_latitudeActivity扩展MapActivity
{
LocationManager流明;
MyLocationListener LocationListener的;
图形页面图形页面;
MapController MC;
GeoPoint对象的GeoPoint;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
尝试{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
的GeoPoint =新的GeoPoint((INT)(12.949997 * 1E6),(INT)(80.140213 * 1E6));
图形页面=(图形页面)findViewById(R.id.name);
MC = mapView.getController();
mc.animateTo(GeoPoint对象);
mapView.setClickable(真正的);
mapView.setBuiltInZoomControls(真正的);
mapView.setSatellite(真正的);
mapView.setTraffic(真正的);
名单<覆盖>叠加=调用MapView.getOverlays();
overlays.clear();
overlays.add(新MyLocationListener());
// ---使用LocationManager类获取GPS位置---
LM =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,新MyLocationListener());
}
赶上(例外五){
// TODO:处理异常
Toast.makeText(getApplicationContext(),e.toString(),1).show();
}
}
@覆盖
保护的布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
私有类MyLocationListener扩展覆盖实现LocationListener的
{
公共无效画(油画画布,图形页面图形页面,布尔影子){// 1
super.draw(帆布,图形页面,阴影);
如果(!阴影){// 2
点对点=新的点();
MapView.getProjection()在toPixels(GeoPoint对象,点)。 // 3
BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.marker_default); // 4
INT X = point.x - bmp.getWidth()/ 2; // 5 INTY = point.ybmp.getHeight(); // 6 canvas.drawBitmap(BMP,X,Y,NULL); // 7
}
}
公共布尔的onTouchEvent(MotionEvent事件,图形页面图形页面)
{
// ---当用户抬起手指---
如果(event.getAction()== 1){
的GeoPoint P = MapView.getProjection()在。fromPixels(
(int)的event.getX(),
(int)的event.getY());
}
返回false;
}
公共布尔中的onTap(GeoPoint对象点,图形页面图形页面)
{
弦乐味精=纬度::+ point.getLatitudeE6()/ 1E6 + - +
朗::+ point.getLongitudeE6()/ 1E6;
Toast.makeText(mapView.getContext(),味精,1).show();
返回true;
}
@覆盖
公共无效onLocationChanged(位置LOC){
如果(LOC!= NULL){
Toast.makeText(mapView.getContext(),
地点改:纬度:+ loc.getLatitude()+
液化天然气+ loc.getLongitude()
1).show();
的GeoPoint P =新的GeoPoint(
(中间体)(loc.getLatitude()* 1E6),
(中间体)(loc.getLongitude()* 1E6));
mc.animateTo(对);
mc.setZoom(15);
mapView.invalidate();
}
}
@覆盖
公共无效onProviderDisabled(字符串提供商){
// TODO自动生成方法存根
Toast.makeText(mapView.getContext(),熙,1).show();
}
@覆盖
公共无效onProviderEnabled(字符串提供商){
// TODO自动生成方法存根
Toast.makeText(mapView.getContext(),熙,1).show();
}
@覆盖
公共无效onStatusChanged(字符串商,INT地位,
捆绑演员){
// TODO自动生成方法存根
Toast.makeText(mapView.getContext(),熙,1).show();
}
}
}
解决方案
首先,你应该补充许可
<使用-权限的Android:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Android:名称=android.permission.ACCESS_FINE_LOCATION/>
<使用-权限的Android:名称=android.permission.INTERNET对>< /使用-许可>
然后尝试,
标准标准=新标准();
字符串bestProvider = locationManager.getBestProvider(标准,真正的);
位置位置= locationManager.getLastKnownLocation(bestProvider);
locationManager.requestLocationUpdates(bestProvider,0,0,新TestLocationListener());
代替
LM =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,新MyLocationListener());
*Please tell me how to set the Geo-point to current location on start of program? I want to set an overlay balloon to current location of user in emulator on starting of the program marked as "you are here". My Code is here-*
package com.jbb;
import java.util.List;
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;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;
public class jbb_latitudeActivity extends MapActivity
{
LocationManager lm;
MyLocationListener locationListener;
MapView mapView;
MapController mc;
GeoPoint geoPoint;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
geoPoint = new GeoPoint( (int) (12.949997* 1E6), (int) (80.140213 * 1E6));
mapView = (MapView) findViewById(R.id.name);
mc = mapView.getController();
mc.animateTo(geoPoint);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
mapView.setTraffic(true);
List<Overlay> overlays = mapView.getOverlays();
overlays.clear();
overlays.add(new MyLocationListener());
//---use the LocationManager class to obtain GPS locations---
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new MyLocationListener());
}
catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(),e.toString() , 1).show();
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private class MyLocationListener extends Overlay implements LocationListener
{
public void draw(Canvas canvas, MapView mapView, boolean shadow) { // 1
super.draw(canvas, mapView, shadow);
if (!shadow) { // 2
Point point = new Point();
mapView.getProjection().toPixels(geoPoint, point); // 3
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker_default); // 4
int x = point.x - bmp.getWidth() / 2; // 5 inty=point.ybmp.getHeight(); // 6 canvas.drawBitmap(bmp,x,y,null); // 7
}
}
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
}
return false;
}
public boolean onTap(GeoPoint point, MapView mapView)
{
String msg = "Lat:: " + point.getLatitudeE6()/1E6 + " - " +
"Lon:: " + point.getLongitudeE6()/1E6;
Toast.makeText(mapView.getContext(), msg,1).show();
return true;
}
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
Toast.makeText(mapView.getContext(),
"Location changed : Lat: " + loc.getLatitude() +
" Lng: " + loc.getLongitude(),
1).show();
GeoPoint p = new GeoPoint(
(int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
mc.animateTo(p);
mc.setZoom(15);
mapView.invalidate();
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(mapView.getContext(), "hee", 1).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(mapView.getContext(), "hee", 1).show();
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
Toast.makeText(mapView.getContext(), "hee", 1).show();
}
}
}
解决方案
First,you should have added the permission
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Then try,
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
locationManager.requestLocationUpdates(bestProvider, 0, 0, new TestLocationListener());
instead of,
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new MyLocationListener());
这篇关于如何在地图的Android当前的位置设置的GeoPoint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文