我已经使用mapView api2在android中为谷歌地图制作了一个应用程序,我已经尝试了以下代码。地图完全运行但我希望在单击片段时使用它,然后应该在secondActivity的webview中加载特定的“url”。我的代码如下:
主.java
package com.example.mapviewdemo;
import java.security.PublicKey;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.webkit.WebView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
private GoogleMap map;
WebView wv;
private String a ;
private String b ;
//public String ah="http://en.wikipedia.org/wiki/Ahmedabad";
//public String br="http://en.wikipedia.org/wiki/Baroda";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
setUpMap();
if (map != null) {
Marker Ahmedabad = map.addMarker(new MarkerOptions().position(
new LatLng(Double.parseDouble("23.03957"), Double
.parseDouble("72.56600"))).title("Ahmedabad"));
Marker Baroda = map.addMarker(new MarkerOptions()
.position(
new LatLng(Double.parseDouble("22.30731"), Double
.parseDouble("73.18110")))
.title("Baroda")
.snippet("Baroda")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.pin_blue)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(Double.parseDouble("23.03957"), Double
.parseDouble("72.56600")), 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
a =Ahmedabad.getTitle();
b = Baroda.getTitle();
}
}
private void setUpMap() {
UiSettings setting = map.getUiSettings();
setting.setMyLocationButtonEnabled(true);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
if(arg0.getTitle().equals(a)){
Toast.makeText(getApplicationContext(),"Ahmedabad", 0).show();
}
else
{
Toast.makeText(getApplicationContext(),"Baroda", 0).show();
}
return false;
}
});
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
if(arg0.getTitle().equals(a)){
Intent i =new Intent(getApplicationContext(),SecondActivity.class);
i.putExtra("ahm", a);
startActivity(i);
}
else{
Intent i =new Intent(getApplicationContext(),SecondActivity.class);
i.putExtra("brd", b);
startActivity(i);
}
//Toast.makeText(getApplicationContext(), "Check",0).show();
}
});
}
}
第二个.java
package com.example.mapviewdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
public class SecondActivity extends Activity {
WebView v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
v=(WebView)findViewById(R.id.webView1);
Bundle b =getIntent().getExtras();
b.getString("ahm");
b.getString("brd");
}
}
主要的.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
第二个.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>