如何添加覆盖项目在我的当前位置在这个code [英] how to add overlay item in my current location in this code
问题描述
下面是我的code!我想在我目前加覆盖项目
位置 !!但似乎是一个问题,我不能任何人可以检查我有什么
添加到我的code只是把覆盖项目出现在我的当前位置,请检查它。
包tryanabtry.opa;
进口的java.util.List;
进口android.app.PendingIntent;
进口android.content.Context;
进口android.content.res.Resources;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Point;
进口android.graphics.drawable.Drawable;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.os.Handler;
进口android.util.Log;
进口android.widget.TextView;
进口android.widget.Toast;
进口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;
进口com.google.android.maps.OverlayItem;
公共类tryanabtry扩展MapActivity
{
私人MapView类MapView类;
私人MapController MC;
INT错误= 100;
的GeoPoint P,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13;
清单<&叠加GT; mapOverlays;
可绘制可绘制,drawable2,drawable3,
drawable4,drawable6,drawable7,drawable8,drawable9,drawable10,drawable11,drawable12,drawable13,drawable5;
HelloItemizedOverlay itemizedOverlay,itemizedOverlay2,
itemizedOverlay3,
itemizedOverlay4,itemizedOverlay5,itemizedOverlay6,itemizedOverlay7,itemizedOverlay8,itemizedOverlay9,itemizedOverlay10,itemizedOverlay11,itemizedOver lay12,itemizedOverlay13;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
尝试{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
/ *使用的LocationManager类获取GPS位置* /
的LocationManager mlocManager =
(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener的mlocListener =新MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,
0,mlocListener);
图形页面=(图形页面)findViewById(R.id.mapView); mapView.setStreetView(真);
mapView.setSatellite(真);
mapView.setBuiltInZoomControls(真);
MC = mapView.getController(); mc.setZoom(12);
addOverLays();
}
赶上(例外五){
Log.d(eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee,e.getMessage());
}
} 公共无效addOverLays(){
的String [] =坐标
{31.216487288475037,29.932637214660645,30.084123015403748,
51.5002,-0.1262,31.337149143218994};
双纬度= 29.98739718380868,LAT2
= 29.98763859272003,lat3 = 29.987574219703674,lat4 = 29.98718498160553;
双对数= 31.442527770886084,LOG2 =
31.44235074520111,log3中= 31.44225418567575,LOG4 = 31.442527770996094;
P =新的GeoPoint((INT)(LAT * 1E6),(INT)(日志* 1E6));
P2 =新的GeoPoint((INT)(LAT2 * 1e6个电子),(INT)(LOG2 * 1e6个电子));
P3 =新的GeoPoint((INT)(lat3 * 1e6个电子),(INT)(log3中* 1e6个电子));
P4 =新的GeoPoint((INT)(lat4 * 1e6个电子),(INT)(LOG4 * 1e6个电子)); mapOverlays =调用MapView.getOverlays();
绘制= this.getResources()getDrawable(R.drawable.vitblue)。
drawable2 = this.getResources()getDrawable(R.drawable.ballon)。
drawable3 = this.getResources()getDrawable(R.drawable.blackaaaaa)。
drawable4 = this.getResources()getDrawable(R.drawable.vitblue)。
itemizedOverlay =新HelloItemizedOverlay(绘制,这一点);
itemizedOverlay2 =新HelloItemizedOverlay(drawable2,这一点);
itemizedOverlay3 =新HelloItemizedOverlay(drawable3,这一点);
itemizedOverlay4 =新HelloItemizedOverlay(drawable4,这一点);
OverlayItem overlayitem =新OverlayItem(对,开罗,over1);
OverlayItem over2 =新OverlayItem(P2,乌尔姆,over2);
OverlayItem over3 =新OverlayItem(P3,offff,over3);
OverlayItem over4 =新OverlayItem(P4,offff,over4);
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
itemizedOverlay2.addOverlay(over2);
mapOverlays.add(itemizedOverlay2);
itemizedOverlay3.addOverlay(over3);
mapOverlays.add(itemizedOverlay3);
itemizedOverlay4.addOverlay(over4);
mapOverlays.add(itemizedOverlay4);
mc.setZoom(17);
} 公共类MyLocationListener实现LocationListener的
{
@覆盖
公共无效onLocationChanged(位置LOC)
{
的GeoPoint点=新的GeoPoint((INT)(loc.getLatitude()*
1E6)
(中间体)(loc.getLongitude()* 1E6));
///////////////////////////////////我的新的编辑\\\\\\\\\\\\\\\\\\\\\\\\ \\
mapOverlays =调用MapView.getOverlays();
drawable11 = this.getResources()getDrawable(R.drawable.vitblue)。
itemizedOverlay11 =新HelloItemizedOverlay(drawable11,这一点);
OverlayItem over11 =新OverlayItem(点,offff,over5);
itemizedOverlay11.addOverlay(over11);
mapOverlays.add(itemizedOverlay11);
////////////////////////////////////////////////// ///////////////////// /////////构造HelloItemizedOverlay(可绘制,tryanabtry.MyLocationListener)是未定义//////错误
字符串文本=我目前的位置是:+
Latitud =+ loc.getLatitude()+
Longitud =+ loc.getLongitude();
Toast.makeText(getApplicationContext(),
文本,
Toast.LENGTH_SHORT).show();
mc.animateTo(点);
} 私人资源getResources(){
// TODO自动生成方法存根
返回null;
} 私人无效DoubletoString(双纬){
// TODO自动生成方法存根
} 公共无效onProviderDisabled(字符串提供商)
{
Toast.makeText(getApplicationContext(),
全球定位系统已禁用,
Toast.LENGTH_SHORT).show();
} 公共无效onProviderEnabled(字符串提供商)
{
Toast.makeText(getApplicationContext(),
GPS功能
Toast.LENGTH_SHORT).show();
} 公共无效onStatusChanged(字符串提供商,INT的地位,捆绑
演员)
{
} 保护布尔isRouteDisplayed(){
返回false;
}
} / *类MyLocationListener结束* / @覆盖
保护布尔isRouteDisplayed(){
// TODO自动生成方法存根
返回false;
}
}
/ * UseGps活动结束* /
我HelloOverlay班组长
包tryanabtry.opa;
进口的java.util.ArrayList;
进口tryanabtry.opa.tryanabtry.MyLocationListener;
进口android.app.AlertDialog;
进口android.app.AlertDialog.Builder;
进口android.content.res.Resources;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.Path;
进口android.graphics.Point;
进口android.graphics.drawable.Drawable;
进口android.util.Log;
进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.ItemizedOverlay;
进口com.google.android.maps.MapView;
进口com.google.android.maps.Overlay;
进口com.google.android.maps.OverlayItem;
进口com.google.android.maps.Projection;
公共类HelloItemizedOverlay扩展ItemizedOverlay< OverlayItem>
{
私人的ArrayList< OverlayItem> mOverlays =新
ArrayList的< OverlayItem>();
tryanabtry M =新tryanabtry();
公共HelloItemizedOverlay(可绘制defaultMarker,tryanabtry N){
超(boundCenterBottom(defaultMarker));
M = N;
}
@覆盖
保护OverlayItem createItem中(int i)以{
返回mOverlays.get(ⅰ);
}
@覆盖
公众诠释大小(){
返回mOverlays.size();
}
公共无效addOverlay(OverlayItem叠加){
mOverlays.add(覆盖);
填充();
}
// //////////
公共无效画(android.graphics.Canvas帆布,MapView类MapView类,
布尔影子){
类MapOverlay扩展覆盖{
私人的GeoPoint pointToDraw;
公共无效setPointToDraw(GeoPoint的点){
pointToDraw =点;
}
公众的GeoPoint getPointToDraw(){
返回pointToDraw;
}
@覆盖
公共布尔平局(帆布帆布,MapView类MapView类,布尔阴影,
时长){
super.draw(画布,MapView类,阴影);
//转换点像素
点screenPts =新点();
。MapView.getProjection()在toPixels(pointToDraw,screenPts);
//添加标记
BMP位图= BitmapFactory.de codeResource(getResources()
R.drawable.ballon);
canvas.drawBitmap(BMP,screenPts.x,screenPts.y - 24,空)
返回true;
}
私人资源getResources(){
// TODO自动生成方法存根
返回null;
}
}
GeoPoint的P =新的GeoPoint((INT)(29.987574219703674 * 1E6)
(中间体)(31.44225418567575 * 1E6));
GeoPoint的P2 =新的GeoPoint((INT)(29.98763859272003 * 1E6)
(中间体)(31.44235074520111 * 1E6));
GeoPoint的P3 =新的GeoPoint((INT)(29.98718498160553 * 1E6)
(中间体)(31.442527770996094 * 1E6));
P4的GeoPoint =新的GeoPoint((INT)(29.98739718380868 * 1E6)
(中间体)(31.442527770886084 * 1E6));
//假设你现在已经赋值给这两个GeoPoints。
投影投影= MapView.getProjection()在;
点起点= projection.toPixels(P,NULL);
点startingPoint1 = projection.toPixels(P,NULL);
点startingPoint2 = projection.toPixels(P2,NULL);
点endingPoint = projection.toPixels(P2,NULL);
点endingPoint1 = projection.toPixels(P3,NULL);
点endingPoint2 = projection.toPixels(P4,NULL);
//创建包含两个点之间的线的路径。
路径path =新路径();
path.moveTo(startingPoint.x,startingPoint.y);
path.lineTo(endingPoint.x,endingPoint.y);
路径PATH1 =新路径();
path1.moveTo(startingPoint1.x,startingPoint1.y);
path1.lineTo(endingPoint1.x,endingPoint1.y);
路径PATH2 =新路径();
path2.moveTo(startingPoint2.x,startingPoint2.y);
path2.lineTo(endingPoint2.x,endingPoint2.y); 涂料粉刷=新的油漆();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
涂料paint1 =新的油漆();
paint1.setStyle(Paint.Style.STROKE);
paint1.setColor(Color.GREEN);
涂料paint2 =新的油漆();
paint2.setStyle(Paint.Style.STROKE);
paint2.setColor(Color.YELLOW);
//绘制的路径!
super.draw(画布,MapView类,阴影);
//埃尔超德3shnnn到ADDD koloooo覆盖W¯¯路径
canvas.drawPath(路径,油漆);
canvas.drawPath(PATH1,paint1);
canvas.drawPath(PATH2,paint2);
}
@覆盖
公共布尔中的onTap(int i)以{
尝试{
字符串s = mOverlays.get(0).getSnippet();
串[]的相关信息= s.split(,);
建设者建设者=新AlertDialog.Builder(米);
builder.setTitle(关于信息:+的相关信息[0]);
builder.setIcon(m.getResources()。getDrawable(
m.getResources()。则getIdentifier(mOverlays.get(0).getTitle(),
可拉伸,net.learn2develop.GoogleMaps)));
builder.setMessage(年龄:+的相关信息[1] +\\ njob:+的相关信息[2]
+\\ ninstitute:+的相关信息[3]);
builder.setPositiveButton(OK,NULL);
builder.show();
}
赶上(例外五){
Log.d(3'lt,lihh雅WDI3,E);
}
返回true;
}
我迟到了,但原因你得到的错误:
构造HelloItemizedOverlay(可绘制,tryanabtry.MyLocationListener)未定义
块引用>是因为你使用
的这种
调用HelloItemizedOverlay
的构造函数时。在此背景下,这个
指的实例MyLocationListener
其中code是内,而不是一个实例之类的tryanabtry
。替换此行:
itemizedOverlay11 =新HelloItemizedOverlay(drawable11,这一点);
本:
itemizedOverlay11 =新HelloItemizedOverlay(drawable11,tryanabtry.this);
另外请注意,按照约定的Java类应该以大写字母开头这样
Tryanabtry
而不是tryanabtry
。Here is my code !! i want to add overlay item in my current location !! but seems a problem i cant can any one check what i have to add to my code to just put overlay item appear in my current location Please Check it up .
package tryanabtry.opa; import java.util.List; import android.app.PendingIntent; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.widget.TextView; import android.widget.Toast; 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 com.google.android.maps.OverlayItem; public class tryanabtry extends MapActivity { private MapView mapView; private MapController mc; int error=100; GeoPoint p, p2, p3, p4,p5,p6,p7,p8,p9,p10,p11,p12,p13; List<Overlay> mapOverlays; Drawable drawable, drawable2 , drawable3, drawable4,drawable6,drawable7,drawable8,drawable9,drawable10,drawable11,drawable12,drawable13,drawable5; HelloItemizedOverlay itemizedOverlay, itemizedOverlay2 , itemizedOverlay3, itemizedOverlay4,itemizedOverlay5,itemizedOverlay6,itemizedOverlay7,itemizedOverlay8,itemizedOverlay9,itemizedOverlay10,itemizedOverlay11,itemizedOver lay12,itemizedOverlay13; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { try{ super.onCreate(savedInstanceState); setContentView(R.layout.main); /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); mapView = (MapView) findViewById(R.id.mapView); mapView.setStreetView(true); mapView.setSatellite(true); mapView.setBuiltInZoomControls(true); mc = mapView.getController(); mc.setZoom(12); addOverLays(); } catch(Exception e){ Log.d("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",e.getMessage()); } } public void addOverLays(){ String [] coordinates = {"31.216487288475037","29.932637214660645" ,"30.084123015403748", "51.5002" , "-0.1262","31.337149143218994"}; double lat = 29.98739718380868,lat2 =29.98763859272003,lat3=29.987574219703674,lat4=29.98718498160553; double log = 31.442527770886084, log2 = 31.44235074520111,log3=31.44225418567575,log4=31.442527770996094; p = new GeoPoint((int) (lat * 1E6), (int) (log * 1E6)); p2 = new GeoPoint( (int) (lat2 * 1e6), (int) (log2 * 1e6)); p3=new GeoPoint( (int) (lat3 * 1e6), (int) (log3 * 1e6)); p4=new GeoPoint( (int) (lat4 * 1e6), (int) (log4 * 1e6)); mapOverlays = mapView.getOverlays(); drawable = this.getResources().getDrawable(R.drawable.vitblue); drawable2 = this.getResources().getDrawable(R.drawable.ballon); drawable3 = this.getResources().getDrawable(R.drawable.blackaaaaa); drawable4 = this.getResources().getDrawable(R.drawable.vitblue); itemizedOverlay = new HelloItemizedOverlay(drawable,this); itemizedOverlay2 = new HelloItemizedOverlay(drawable2,this); itemizedOverlay3 = new HelloItemizedOverlay(drawable3,this); itemizedOverlay4 = new HelloItemizedOverlay(drawable4,this); OverlayItem overlayitem = new OverlayItem(p, "Cairo", " over1"); OverlayItem over2 = new OverlayItem(p2, "ulm", "over2"); OverlayItem over3 = new OverlayItem(p3, "offff", "over3"); OverlayItem over4 = new OverlayItem(p4, "offff", "over4"); itemizedOverlay.addOverlay(overlayitem); mapOverlays.add(itemizedOverlay); itemizedOverlay2.addOverlay(over2); mapOverlays.add(itemizedOverlay2); itemizedOverlay3.addOverlay(over3); mapOverlays.add(itemizedOverlay3); itemizedOverlay4.addOverlay(over4); mapOverlays.add(itemizedOverlay4); mc.setZoom(17); } public class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { GeoPoint point = new GeoPoint( (int) (loc.getLatitude() * 1E6), (int) (loc.getLongitude() * 1E6)); ///////////////////////////////////MY NEW EDIT \\\\\\\\\\\\\ mapOverlays = mapView.getOverlays(); drawable11 = this.getResources().getDrawable(R.drawable.vitblue); itemizedOverlay11= new HelloItemizedOverlay(drawable11,this); OverlayItem over11 = new OverlayItem(point, "offff", "over5"); itemizedOverlay11.addOverlay(over11); mapOverlays.add(itemizedOverlay11); /////////////////////////////////////////////////////////////////////// /////////The constructor HelloItemizedOverlay(Drawable, tryanabtry.MyLocationListener) is undefined ////// error String Text = "My current location is: " + "Latitud ="+ loc.getLatitude() + "Longitud =" + loc.getLongitude(); Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show(); mc.animateTo(point); } private Resources getResources() { // TODO Auto-generated method stub return null; } private void DoubletoString(double latitude) { // TODO Auto-generated method stub } public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } public void onStatusChanged(String provider, int status, Bundle extras) { } protected boolean isRouteDisplayed() { return false; } }/* End of Class MyLocationListener */ @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } } /* End of UseGps Activity*/
My HelloOverlay classs
package tryanabtry.opa; import java.util.ArrayList; import tryanabtry.opa.tryanabtry.MyLocationListener; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.util.Log; import com.google.android.maps.GeoPoint; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import com.google.android.maps.OverlayItem; import com.google.android.maps.Projection; public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> { private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); tryanabtry m = new tryanabtry(); public HelloItemizedOverlay(Drawable defaultMarker, tryanabtry n) { super(boundCenterBottom(defaultMarker)); m = n; } @Override protected OverlayItem createItem(int i) { return mOverlays.get(i); } @Override public int size() { return mOverlays.size(); } public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); } // ////////// public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) { class MapOverlay extends Overlay { private GeoPoint pointToDraw; public void setPointToDraw(GeoPoint point) { pointToDraw = point; } public GeoPoint getPointToDraw() { return pointToDraw; } @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { super.draw(canvas, mapView, shadow); // convert point to pixels Point screenPts = new Point(); mapView.getProjection().toPixels(pointToDraw, screenPts); // add marker Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ballon); canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null) return true; } private Resources getResources() { // TODO Auto-generated method stub return null; } } GeoPoint p = new GeoPoint((int) (29.987574219703674 * 1E6), (int) (31.44225418567575 * 1E6)); GeoPoint p2 = new GeoPoint((int) ( 29.98763859272003 * 1E6), (int) (31.44235074520111 * 1E6)); GeoPoint p3 = new GeoPoint((int) (29.98718498160553* 1E6), (int) (31.442527770996094 * 1E6)); GeoPoint p4= new GeoPoint((int) (29.98739718380868 * 1E6), (int) (31.442527770886084 * 1E6)); // Let's assume you've assigned values to these two GeoPoints now. Projection projection = mapView.getProjection(); Point startingPoint = projection.toPixels(p, null); Point startingPoint1 = projection.toPixels(p, null); Point startingPoint2= projection.toPixels(p2, null); Point endingPoint = projection.toPixels(p2, null); Point endingPoint1 = projection.toPixels(p3, null); Point endingPoint2= projection.toPixels(p4, null); // Create the path containing the line between the two points. Path path = new Path(); path.moveTo(startingPoint.x, startingPoint.y); path.lineTo(endingPoint.x, endingPoint.y); Path path1 = new Path(); path1.moveTo(startingPoint1.x, startingPoint1.y); path1.lineTo(endingPoint1.x, endingPoint1.y); Path path2 = new Path(); path2.moveTo(startingPoint2.x, startingPoint2.y); path2.lineTo(endingPoint2.x, endingPoint2.y); Paint paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.RED); Paint paint1 = new Paint(); paint1.setStyle(Paint.Style.STROKE); paint1.setColor(Color.GREEN); Paint paint2 = new Paint(); paint2.setStyle(Paint.Style.STROKE); paint2.setColor(Color.YELLOW); // Draw the path! super.draw(canvas, mapView, shadow); // el super de 3shnnn to addd koloooo overlay w path canvas.drawPath(path, paint); canvas.drawPath(path1, paint1); canvas.drawPath(path2, paint2); } @Override public boolean onTap(int i) { try { String s = mOverlays.get(0).getSnippet(); String[] infos = s.split(","); Builder builder = new AlertDialog.Builder(m); builder.setTitle("Information about: " + infos[0]); builder.setIcon(m.getResources().getDrawable( m.getResources().getIdentifier(mOverlays.get(0).getTitle(), "drawable", "net.learn2develop.GoogleMaps"))); builder.setMessage("age: " + infos[1] + "\njob: " + infos[2] + "\ninstitute: " + infos[3]); builder.setPositiveButton("ok", null); builder.show(); } catch (Exception e) { Log.d("3'lt", "lihh ya wdi3", e); } return true; }
解决方案I'm late to the party, but the reason you are getting the error:
The constructor HelloItemizedOverlay(Drawable, tryanabtry.MyLocationListener) is undefined
is because of your use of
this
when calling the constructor ofHelloItemizedOverlay
. In this context,this
refers to the instance ofMyLocationListener
which the code is within, and not an instance of the classtryanabtry
.Replace this line:
itemizedOverlay11= new HelloItemizedOverlay(drawable11,this);
with this:
itemizedOverlay11= new HelloItemizedOverlay(drawable11,tryanabtry.this);
Please also note that by convention Java classes should start with a capital letter, so
Tryanabtry
instead oftryanabtry
.这篇关于如何添加覆盖项目在我的当前位置在这个code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!